Show notice about "never used" for enum
This commit is contained in:
parent
5639c21fb3
commit
d630f5da7a
3 changed files with 55 additions and 1 deletions
|
@ -1011,6 +1011,22 @@ impl<'tcx> DeadVisitor<'tcx> {
|
||||||
parent_item: Option<LocalDefId>,
|
parent_item: Option<LocalDefId>,
|
||||||
report_on: ReportOn,
|
report_on: ReportOn,
|
||||||
) {
|
) {
|
||||||
|
fn get_parent_if_enum_variant<'tcx>(
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
|
may_variant: LocalDefId,
|
||||||
|
) -> LocalDefId {
|
||||||
|
if let Node::Variant(_) = tcx.hir_node_by_def_id(may_variant)
|
||||||
|
&& let Some(enum_did) = tcx.opt_parent(may_variant.to_def_id())
|
||||||
|
&& let Some(enum_local_id) = enum_did.as_local()
|
||||||
|
&& let Node::Item(item) = tcx.hir_node_by_def_id(enum_local_id)
|
||||||
|
&& let ItemKind::Enum(_, _) = item.kind
|
||||||
|
{
|
||||||
|
enum_local_id
|
||||||
|
} else {
|
||||||
|
may_variant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let Some(&first_item) = dead_codes.first() else {
|
let Some(&first_item) = dead_codes.first() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
@ -1054,6 +1070,9 @@ impl<'tcx> DeadVisitor<'tcx> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let encl_def_id = parent_item.unwrap_or(first_item.def_id);
|
let encl_def_id = parent_item.unwrap_or(first_item.def_id);
|
||||||
|
// If parent of encl_def_id is an enum, use the parent ID intead.
|
||||||
|
let encl_def_id = get_parent_if_enum_variant(tcx, encl_def_id);
|
||||||
|
|
||||||
let ignored_derived_impls =
|
let ignored_derived_impls =
|
||||||
if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) {
|
if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) {
|
||||||
let trait_list = ign_traits
|
let trait_list = ign_traits
|
||||||
|
|
|
@ -6,7 +6,22 @@ enum Enum {
|
||||||
Variant2,
|
Variant2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum TupleVariant {
|
||||||
|
Variant1(i32), //~ ERROR: variant `Variant1` is never constructed
|
||||||
|
Variant2,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum StructVariant {
|
||||||
|
Variant1 { id: i32 }, //~ ERROR: variant `Variant1` is never constructed
|
||||||
|
Variant2,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let e = Enum::Variant2;
|
let e = Enum::Variant2;
|
||||||
e.clone();
|
e.clone();
|
||||||
|
|
||||||
|
let _ = TupleVariant::Variant2;
|
||||||
|
let _ = StructVariant::Variant2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,5 +13,25 @@ note: the lint level is defined here
|
||||||
LL | #![deny(dead_code)]
|
LL | #![deny(dead_code)]
|
||||||
| ^^^^^^^^^
|
| ^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
error: variant `Variant1` is never constructed
|
||||||
|
--> $DIR/unused-variant.rs:11:5
|
||||||
|
|
|
||||||
|
LL | enum TupleVariant {
|
||||||
|
| ------------ variant in this enum
|
||||||
|
LL | Variant1(i32),
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `TupleVariant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
|
||||||
|
|
||||||
|
error: variant `Variant1` is never constructed
|
||||||
|
--> $DIR/unused-variant.rs:17:5
|
||||||
|
|
|
||||||
|
LL | enum StructVariant {
|
||||||
|
| ------------- variant in this enum
|
||||||
|
LL | Variant1 { id: i32 },
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `StructVariant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue