@@ -14,7 +14,7 @@ use rustc_errors::MultiSpan;
1414use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
1515use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId } ;
1616use rustc_hir:: intravisit:: { self , Visitor } ;
17- use rustc_hir:: { self as hir, Node , PatKind , QPath , TyKind } ;
17+ use rustc_hir:: { self as hir, ImplItem , ImplItemKind , Node , PatKind , QPath , TyKind } ;
1818use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
1919use rustc_middle:: middle:: privacy:: Level ;
2020use rustc_middle:: query:: Providers ;
@@ -936,7 +936,9 @@ enum ShouldWarnAboutField {
936936
937937#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
938938enum ReportOn {
939+ /// Report on something that hasn't got a proper name to refer to
939940 TupleField ,
941+ /// Report on something that has got a name, which could be a field but also a method
940942 NamedField ,
941943}
942944
@@ -1061,6 +1063,31 @@ impl<'tcx> DeadVisitor<'tcx> {
10611063 None
10621064 } ;
10631065
1066+ let enum_variants_with_same_name = dead_codes
1067+ . iter ( )
1068+ . filter_map ( |dead_item| {
1069+ if let Node :: ImplItem ( ImplItem {
1070+ kind : ImplItemKind :: Fn ( ..) | ImplItemKind :: Const ( ..) ,
1071+ ..
1072+ } ) = tcx. hir_node_by_def_id ( dead_item. def_id )
1073+ && let Some ( impl_did) = tcx. opt_parent ( dead_item. def_id . to_def_id ( ) )
1074+ && let DefKind :: Impl { of_trait : false } = tcx. def_kind ( impl_did)
1075+ && let ty:: Adt ( maybe_enum, _) = tcx. type_of ( impl_did) . skip_binder ( ) . kind ( )
1076+ && maybe_enum. is_enum ( )
1077+ && let Some ( variant) =
1078+ maybe_enum. variants ( ) . iter ( ) . find ( |i| i. name == dead_item. name )
1079+ {
1080+ Some ( crate :: errors:: EnumVariantSameName {
1081+ descr : tcx. def_descr ( dead_item. def_id . to_def_id ( ) ) ,
1082+ dead_name : dead_item. name ,
1083+ variant_span : tcx. def_span ( variant. def_id ) ,
1084+ } )
1085+ } else {
1086+ None
1087+ }
1088+ } )
1089+ . collect ( ) ;
1090+
10641091 let diag = match report_on {
10651092 ReportOn :: TupleField => {
10661093 let tuple_fields = if let Some ( parent_id) = parent_item
@@ -1114,6 +1141,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11141141 name_list,
11151142 parent_info,
11161143 ignored_derived_impls,
1144+ enum_variants_with_same_name,
11171145 } ,
11181146 } ;
11191147
0 commit comments