Skip to content

Commit 3478689

Browse files
committed
add span lint message for multiple types deserialized
1 parent 5346db4 commit 3478689

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lints/type_cosplay/src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,19 @@ impl<'tcx> LateLintPass<'tcx> for TypeCosplay {
8484
_ => check_structs_have_discriminant(cx, v), // NOTE: also catches unions
8585
}
8686
} else {
87-
// Retrieve spans: iter through map, grab first elem of two different key pairs, then get span
88-
// span_lint_and_help(
89-
// cx,
90-
// TYPE_COSPLAY,
91-
// span,
92-
// "Deserializing from multiple ADT types. "
93-
// None,
94-
// "help: deserialize from only structs with a discriminant, or an enum encapsulating all structs."
95-
// )
87+
// Retrieve spans: iter through map, grab first elem of each key-pair, then get span
88+
let mut spans = vec![];
89+
self.deser_types.iter().for_each(|(_, v)| {
90+
spans.push(v[0].1);
91+
});
92+
span_lint_and_help(
93+
cx,
94+
TYPE_COSPLAY,
95+
spans[0],
96+
"Deserializing from multiple ADT types.",
97+
Some(spans[1]),
98+
"help: deserialize from only structs with a discriminant, or an enum encapsulating all structs."
99+
)
96100
}
97101
}
98102
}
@@ -148,7 +152,7 @@ fn check_structs_have_discriminant(cx: &LateContext<'_>, types: &Vec<(DefId, Spa
148152
/// Checks if `adt` has a proper discriminant. We define a proper discriminant as being an enum with
149153
/// the number of variants at least the number of deserialized structs. Further the discriminant should
150154
/// be the first field in the adt.
151-
fn has_discriminant(cx: &LateContext, adt: &AdtDef, num_struct_types: usize, span: Span) {
155+
fn has_discriminant(cx: &LateContext, adt: AdtDef, num_struct_types: usize, span: Span) {
152156
let variant = adt.variants().get(Idx::new(0)).unwrap();
153157
let first_field_def = &variant.fields[0];
154158
let ty = cx.tcx.type_of(first_field_def.did);

0 commit comments

Comments
 (0)