File tree 3 files changed +55
-1
lines changed
compiler/rustc_passes/src
3 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -1010,6 +1010,22 @@ impl<'tcx> DeadVisitor<'tcx> {
1010
1010
parent_item : Option < LocalDefId > ,
1011
1011
report_on : ReportOn ,
1012
1012
) {
1013
+ fn get_parent_if_enum_variant < ' tcx > (
1014
+ tcx : TyCtxt < ' tcx > ,
1015
+ may_variant : LocalDefId ,
1016
+ ) -> LocalDefId {
1017
+ if let Node :: Variant ( _) = tcx. hir_node_by_def_id ( may_variant)
1018
+ && let Some ( enum_did) = tcx. opt_parent ( may_variant. to_def_id ( ) )
1019
+ && let Some ( enum_local_id) = enum_did. as_local ( )
1020
+ && let Node :: Item ( item) = tcx. hir_node_by_def_id ( enum_local_id)
1021
+ && let ItemKind :: Enum ( _, _) = item. kind
1022
+ {
1023
+ enum_local_id
1024
+ } else {
1025
+ may_variant
1026
+ }
1027
+ }
1028
+
1013
1029
let Some ( & first_item) = dead_codes. first ( ) else {
1014
1030
return ;
1015
1031
} ;
@@ -1053,6 +1069,9 @@ impl<'tcx> DeadVisitor<'tcx> {
1053
1069
} ;
1054
1070
1055
1071
let encl_def_id = parent_item. unwrap_or ( first_item. def_id ) ;
1072
+ // If parent of encl_def_id is an enum, use the parent ID intead.
1073
+ let encl_def_id = get_parent_if_enum_variant ( tcx, encl_def_id) ;
1074
+
1056
1075
let ignored_derived_impls =
1057
1076
if let Some ( ign_traits) = self . ignored_derived_traits . get ( & encl_def_id) {
1058
1077
let trait_list = ign_traits
Original file line number Diff line number Diff line change @@ -6,7 +6,22 @@ enum Enum {
6
6
Variant2 ,
7
7
}
8
8
9
+ #[ derive( Debug ) ]
10
+ enum TupleVariant {
11
+ Variant1 ( i32 ) , //~ ERROR: variant `Variant1` is never constructed
12
+ Variant2 ,
13
+ }
14
+
15
+ #[ derive( Debug ) ]
16
+ enum StructVariant {
17
+ Variant1 { id : i32 } , //~ ERROR: variant `Variant1` is never constructed
18
+ Variant2 ,
19
+ }
20
+
9
21
fn main ( ) {
10
22
let e = Enum :: Variant2 ;
11
23
e. clone ( ) ;
24
+
25
+ let _ = TupleVariant :: Variant2 ;
26
+ let _ = StructVariant :: Variant2 ;
12
27
}
Original file line number Diff line number Diff line change @@ -13,5 +13,25 @@ note: the lint level is defined here
13
13
LL | #![deny(dead_code)]
14
14
| ^^^^^^^^^
15
15
16
- error: aborting due to 1 previous error
16
+ error: variant `Variant1` is never constructed
17
+ --> $DIR/unused-variant.rs:11:5
18
+ |
19
+ LL | enum TupleVariant {
20
+ | ------------ variant in this enum
21
+ LL | Variant1(i32),
22
+ | ^^^^^^^^
23
+ |
24
+ = note: `TupleVariant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
25
+
26
+ error: variant `Variant1` is never constructed
27
+ --> $DIR/unused-variant.rs:17:5
28
+ |
29
+ LL | enum StructVariant {
30
+ | ------------- variant in this enum
31
+ LL | Variant1 { id: i32 },
32
+ | ^^^^^^^^
33
+ |
34
+ = note: `StructVariant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
35
+
36
+ error: aborting due to 3 previous errors
17
37
You can’t perform that action at this time.
0 commit comments