5
5
6
6
use std:: mem;
7
7
8
- use hir:: ItemKind ;
9
8
use hir:: def_id:: { LocalDefIdMap , LocalDefIdSet } ;
10
9
use rustc_abi:: FieldIdx ;
11
10
use rustc_data_structures:: fx:: FxIndexSet ;
@@ -14,7 +13,7 @@ use rustc_errors::MultiSpan;
14
13
use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
15
14
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId } ;
16
15
use rustc_hir:: intravisit:: { self , Visitor } ;
17
- use rustc_hir:: { self as hir, ImplItem , ImplItemKind , Node , PatKind , QPath } ;
16
+ use rustc_hir:: { self as hir, Node , PatKind , QPath } ;
18
17
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
19
18
use rustc_middle:: middle:: privacy:: Level ;
20
19
use rustc_middle:: query:: Providers ;
@@ -930,25 +929,7 @@ impl<'tcx> DeadVisitor<'tcx> {
930
929
parent_item : Option < LocalDefId > ,
931
930
report_on : ReportOn ,
932
931
) {
933
- fn get_parent_if_enum_variant < ' tcx > (
934
- tcx : TyCtxt < ' tcx > ,
935
- may_variant : LocalDefId ,
936
- ) -> LocalDefId {
937
- if let Node :: Variant ( _) = tcx. hir_node_by_def_id ( may_variant)
938
- && let Some ( enum_did) = tcx. opt_parent ( may_variant. to_def_id ( ) )
939
- && let Some ( enum_local_id) = enum_did. as_local ( )
940
- && let Node :: Item ( item) = tcx. hir_node_by_def_id ( enum_local_id)
941
- && let ItemKind :: Enum ( ..) = item. kind
942
- {
943
- enum_local_id
944
- } else {
945
- may_variant
946
- }
947
- }
948
-
949
- let Some ( & first_item) = dead_codes. first ( ) else {
950
- return ;
951
- } ;
932
+ let Some ( & first_item) = dead_codes. first ( ) else { return } ;
952
933
let tcx = self . tcx ;
953
934
954
935
let first_lint_level = first_item. level ;
@@ -957,40 +938,40 @@ impl<'tcx> DeadVisitor<'tcx> {
957
938
let names: Vec < _ > = dead_codes. iter ( ) . map ( |item| item. name ) . collect ( ) ;
958
939
let spans: Vec < _ > = dead_codes
959
940
. iter ( )
960
- . map ( |item| match tcx. def_ident_span ( item. def_id ) {
961
- Some ( s) => s. with_ctxt ( tcx. def_span ( item. def_id ) . ctxt ( ) ) ,
962
- None => tcx. def_span ( item. def_id ) ,
941
+ . map ( |item| {
942
+ let span = tcx. def_span ( item. def_id ) ;
943
+ let ident_span = tcx. def_ident_span ( item. def_id ) ;
944
+ // FIXME(cjgillot) this SyntaxContext manipulation does not make any sense.
945
+ ident_span. map ( |s| s. with_ctxt ( span. ctxt ( ) ) ) . unwrap_or ( span)
963
946
} )
964
947
. collect ( ) ;
965
948
966
- let descr = tcx. def_descr ( first_item. def_id . to_def_id ( ) ) ;
949
+ let mut descr = tcx. def_descr ( first_item. def_id . to_def_id ( ) ) ;
967
950
// `impl` blocks are "batched" and (unlike other batching) might
968
951
// contain different kinds of associated items.
969
- let descr = if dead_codes. iter ( ) . any ( |item| tcx. def_descr ( item. def_id . to_def_id ( ) ) != descr)
970
- {
971
- "associated item"
972
- } else {
973
- descr
974
- } ;
952
+ if dead_codes. iter ( ) . any ( |item| tcx. def_descr ( item. def_id . to_def_id ( ) ) != descr) {
953
+ descr = "associated item"
954
+ }
955
+
975
956
let num = dead_codes. len ( ) ;
976
957
let multiple = num > 6 ;
977
958
let name_list = names. into ( ) ;
978
959
979
- let parent_info = if let Some ( parent_item) = parent_item {
960
+ let parent_info = parent_item . map ( | parent_item| {
980
961
let parent_descr = tcx. def_descr ( parent_item. to_def_id ( ) ) ;
981
962
let span = if let DefKind :: Impl { .. } = tcx. def_kind ( parent_item) {
982
963
tcx. def_span ( parent_item)
983
964
} else {
984
965
tcx. def_ident_span ( parent_item) . unwrap ( )
985
966
} ;
986
- Some ( ParentInfo { num, descr, parent_descr, span } )
987
- } else {
988
- None
989
- } ;
967
+ ParentInfo { num, descr, parent_descr, span }
968
+ } ) ;
990
969
991
- let encl_def_id = parent_item. unwrap_or ( first_item. def_id ) ;
992
- // If parent of encl_def_id is an enum, use the parent ID instead.
993
- let encl_def_id = get_parent_if_enum_variant ( tcx, encl_def_id) ;
970
+ let mut encl_def_id = parent_item. unwrap_or ( first_item. def_id ) ;
971
+ // `ignored_derived_traits` is computed for the enum, not for the variants.
972
+ if let DefKind :: Variant = tcx. def_kind ( encl_def_id) {
973
+ encl_def_id = tcx. local_parent ( encl_def_id) ;
974
+ }
994
975
995
976
let ignored_derived_impls =
996
977
self . ignored_derived_traits . get ( & encl_def_id) . map ( |ign_traits| {
@@ -1006,31 +987,6 @@ impl<'tcx> DeadVisitor<'tcx> {
1006
987
}
1007
988
} ) ;
1008
989
1009
- let enum_variants_with_same_name = dead_codes
1010
- . iter ( )
1011
- . filter_map ( |dead_item| {
1012
- if let Node :: ImplItem ( ImplItem {
1013
- kind : ImplItemKind :: Fn ( ..) | ImplItemKind :: Const ( ..) ,
1014
- ..
1015
- } ) = tcx. hir_node_by_def_id ( dead_item. def_id )
1016
- && let Some ( impl_did) = tcx. opt_parent ( dead_item. def_id . to_def_id ( ) )
1017
- && let DefKind :: Impl { of_trait : false } = tcx. def_kind ( impl_did)
1018
- && let ty:: Adt ( maybe_enum, _) = tcx. type_of ( impl_did) . skip_binder ( ) . kind ( )
1019
- && maybe_enum. is_enum ( )
1020
- && let Some ( variant) =
1021
- maybe_enum. variants ( ) . iter ( ) . find ( |i| i. name == dead_item. name )
1022
- {
1023
- Some ( crate :: errors:: EnumVariantSameName {
1024
- dead_descr : tcx. def_descr ( dead_item. def_id . to_def_id ( ) ) ,
1025
- dead_name : dead_item. name ,
1026
- variant_span : tcx. def_span ( variant. def_id ) ,
1027
- } )
1028
- } else {
1029
- None
1030
- }
1031
- } )
1032
- . collect ( ) ;
1033
-
1034
990
let diag = match report_on {
1035
991
ReportOn :: TupleField => {
1036
992
let tuple_fields = if let Some ( parent_id) = parent_item
@@ -1076,16 +1032,42 @@ impl<'tcx> DeadVisitor<'tcx> {
1076
1032
ignored_derived_impls,
1077
1033
}
1078
1034
}
1079
- ReportOn :: NamedField => MultipleDeadCodes :: DeadCodes {
1080
- multiple,
1081
- num,
1082
- descr,
1083
- participle,
1084
- name_list,
1085
- parent_info,
1086
- ignored_derived_impls,
1087
- enum_variants_with_same_name,
1088
- } ,
1035
+ ReportOn :: NamedField => {
1036
+ let enum_variants_with_same_name = dead_codes
1037
+ . iter ( )
1038
+ . filter_map ( |dead_item| {
1039
+ if let DefKind :: AssocFn | DefKind :: AssocConst =
1040
+ tcx. def_kind ( dead_item. def_id )
1041
+ && let impl_did = tcx. local_parent ( dead_item. def_id )
1042
+ && let DefKind :: Impl { of_trait : false } = tcx. def_kind ( impl_did)
1043
+ && let ty:: Adt ( maybe_enum, _) =
1044
+ tcx. type_of ( impl_did) . instantiate_identity ( ) . kind ( )
1045
+ && maybe_enum. is_enum ( )
1046
+ && let Some ( variant) =
1047
+ maybe_enum. variants ( ) . iter ( ) . find ( |i| i. name == dead_item. name )
1048
+ {
1049
+ Some ( crate :: errors:: EnumVariantSameName {
1050
+ dead_descr : tcx. def_descr ( dead_item. def_id . to_def_id ( ) ) ,
1051
+ dead_name : dead_item. name ,
1052
+ variant_span : tcx. def_span ( variant. def_id ) ,
1053
+ } )
1054
+ } else {
1055
+ None
1056
+ }
1057
+ } )
1058
+ . collect ( ) ;
1059
+
1060
+ MultipleDeadCodes :: DeadCodes {
1061
+ multiple,
1062
+ num,
1063
+ descr,
1064
+ participle,
1065
+ name_list,
1066
+ parent_info,
1067
+ ignored_derived_impls,
1068
+ enum_variants_with_same_name,
1069
+ }
1070
+ }
1089
1071
} ;
1090
1072
1091
1073
let hir_id = tcx. local_def_id_to_hir_id ( first_item. def_id ) ;
0 commit comments