@@ -635,8 +635,7 @@ fn live_symbols_and_ignored_derived_traits<'tcx>(
635635}
636636
637637struct DeadVariant {
638- hir_id : hir:: HirId ,
639- span : Span ,
638+ def_id : LocalDefId ,
640639 name : Symbol ,
641640 level : lint:: Level ,
642641}
@@ -687,29 +686,39 @@ impl<'tcx> DeadVisitor<'tcx> {
687686
688687 fn warn_multiple_dead_codes (
689688 & self ,
690- dead_codes : & [ ( hir :: HirId , Span , Symbol ) ] ,
689+ dead_codes : & [ LocalDefId ] ,
691690 participle : & str ,
692- parent_hir_id : Option < hir :: HirId > ,
691+ parent_item : Option < LocalDefId > ,
693692 ) {
694- if let Some ( ( id, _, name) ) = dead_codes. first ( )
695- && !name. as_str ( ) . starts_with ( '_' )
696- {
697- self . tcx . struct_span_lint_hir (
693+ if let Some ( & first_id) = dead_codes. first ( ) {
694+ let tcx = self . tcx ;
695+ let names: Vec < _ > = dead_codes
696+ . iter ( )
697+ . map ( |& def_id| tcx. item_name ( def_id. to_def_id ( ) ) . to_string ( ) )
698+ . collect ( ) ;
699+ let spans = dead_codes
700+ . iter ( )
701+ . map ( |& def_id| match tcx. def_ident_span ( def_id) {
702+ Some ( s) => s. with_ctxt ( tcx. def_span ( def_id) . ctxt ( ) ) ,
703+ None => tcx. def_span ( def_id) ,
704+ } )
705+ . collect ( ) ;
706+
707+ tcx. struct_span_lint_hir (
698708 lint:: builtin:: DEAD_CODE ,
699- * id,
700- MultiSpan :: from_spans (
701- dead_codes. iter ( ) . map ( |( _, span, _) | * span) . collect ( ) ,
702- ) ,
709+ tcx. hir ( ) . local_def_id_to_hir_id ( first_id) ,
710+ MultiSpan :: from_spans ( spans) ,
703711 |lint| {
704- let def_id = self . tcx . hir ( ) . local_def_id ( * id) ;
705- let descr = self . tcx . def_kind ( def_id) . descr ( def_id. to_def_id ( ) ) ;
712+ let descr = tcx. def_kind ( first_id) . descr ( first_id. to_def_id ( ) ) ;
706713 let span_len = dead_codes. len ( ) ;
707- let names = match & dead_codes. iter ( ) . map ( |( _, _, n) | n. to_string ( ) ) . collect :: < Vec < _ > > ( ) [ ..]
708- {
714+ let names = match & names[ ..] {
709715 _ if span_len > 6 => String :: new ( ) ,
710716 [ name] => format ! ( "`{name}` " ) ,
711717 [ names @ .., last] => {
712- format ! ( "{} and `{last}` " , names. iter( ) . map( |name| format!( "`{name}`" ) ) . join( ", " ) )
718+ format ! (
719+ "{} and `{last}` " ,
720+ names. iter( ) . map( |name| format!( "`{name}`" ) ) . join( ", " )
721+ )
713722 }
714723 [ ] => unreachable ! ( ) ,
715724 } ;
@@ -719,25 +728,17 @@ impl<'tcx> DeadVisitor<'tcx> {
719728 s = pluralize!( span_len) ,
720729 are = pluralize!( "is" , span_len) ,
721730 ) ) ;
722- let hir = self . tcx . hir ( ) ;
723- if let Some ( parent_hir_id) = parent_hir_id
724- && let Some ( parent_node) = hir. find ( parent_hir_id)
725- && let Node :: Item ( item) = parent_node
726- {
727- let def_id = self . tcx . hir ( ) . local_def_id ( parent_hir_id) ;
728- let parent_descr = self . tcx . def_kind ( def_id) . descr ( def_id. to_def_id ( ) ) ;
731+
732+ if let Some ( parent_item) = parent_item {
733+ let parent_descr = tcx. def_kind ( parent_item) . descr ( parent_item. to_def_id ( ) ) ;
729734 err. span_label (
730- item. ident . span ,
731- format ! (
732- "{descr}{s} in this {parent_descr}" ,
733- s = pluralize!( span_len)
734- ) ,
735+ tcx. def_ident_span ( parent_item) . unwrap ( ) ,
736+ format ! ( "{descr}{s} in this {parent_descr}" , s = pluralize!( span_len) ) ,
735737 ) ;
736738 }
737- if let Some ( encl_scope) = hir. get_enclosing_scope ( * id)
738- && let Some ( encl_def_id) = hir. opt_local_def_id ( encl_scope)
739- && let Some ( ign_traits) = self . ignored_derived_traits . get ( & encl_def_id)
740- {
739+
740+ let encl_def_id = parent_item. unwrap_or ( first_id) ;
741+ if let Some ( ign_traits) = self . ignored_derived_traits . get ( & encl_def_id) {
741742 let traits_str = ign_traits
742743 . iter ( )
743744 . map ( |( trait_id, _) | format ! ( "`{}`" , self . tcx. item_name( * trait_id) ) )
@@ -758,15 +759,15 @@ impl<'tcx> DeadVisitor<'tcx> {
758759 ) ;
759760 err. note ( & msg) ;
760761 }
761- err. emit ( ) ;
762- } ,
762+ err. emit ( ) ;
763+ } ,
763764 ) ;
764765 }
765766 }
766767
767768 fn warn_dead_fields_and_variants (
768769 & self ,
769- hir_id : hir :: HirId ,
770+ def_id : LocalDefId ,
770771 participle : & str ,
771772 dead_codes : Vec < DeadVariant > ,
772773 ) {
@@ -781,23 +782,18 @@ impl<'tcx> DeadVisitor<'tcx> {
781782 dead_codes. sort_by_key ( |v| v. level ) ;
782783 for ( _, group) in & dead_codes. into_iter ( ) . group_by ( |v| v. level ) {
783784 self . warn_multiple_dead_codes (
784- & group
785- . map ( |v| ( v. hir_id , v. span , v. name ) )
786- . collect :: < Vec < ( hir:: HirId , Span , Symbol ) > > ( ) ,
785+ & group. map ( |v| v. def_id ) . collect :: < Vec < _ > > ( ) ,
787786 participle,
788- Some ( hir_id ) ,
787+ Some ( def_id ) ,
789788 ) ;
790789 }
791790 }
792791
793- fn warn_dead_code (
794- & mut self ,
795- id : hir:: HirId ,
796- span : rustc_span:: Span ,
797- name : Symbol ,
798- participle : & str ,
799- ) {
800- self . warn_multiple_dead_codes ( & [ ( id, span, name) ] , participle, None ) ;
792+ fn warn_dead_code ( & mut self , id : LocalDefId , participle : & str ) {
793+ if self . tcx . item_name ( id. to_def_id ( ) ) . as_str ( ) . starts_with ( '_' ) {
794+ return ;
795+ }
796+ self . warn_multiple_dead_codes ( & [ id] , participle, None ) ;
801797 }
802798}
803799
@@ -815,33 +811,11 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
815811 fn visit_item ( & mut self , item : & ' tcx hir:: Item < ' tcx > ) {
816812 if self . should_warn_about_item ( item) {
817813 // For most items, we want to highlight its identifier
818- let span = match item. kind {
819- hir:: ItemKind :: Fn ( ..)
820- | hir:: ItemKind :: Mod ( ..)
821- | hir:: ItemKind :: Enum ( ..)
822- | hir:: ItemKind :: Struct ( ..)
823- | hir:: ItemKind :: Union ( ..)
824- | hir:: ItemKind :: Trait ( ..)
825- | hir:: ItemKind :: Impl { .. } => {
826- // FIXME(66095): Because item.span is annotated with things
827- // like expansion data, and ident.span isn't, we use the
828- // def_span method if it's part of a macro invocation
829- // (and thus has a source_callee set).
830- // We should probably annotate ident.span with the macro
831- // context, but that's a larger change.
832- if item. span . source_callee ( ) . is_some ( ) {
833- self . tcx . sess . source_map ( ) . guess_head_span ( item. span )
834- } else {
835- item. ident . span
836- }
837- }
838- _ => item. span ,
839- } ;
840814 let participle = match item. kind {
841815 hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
842816 _ => "used" ,
843817 } ;
844- self . warn_dead_code ( item. hir_id ( ) , span , item . ident . name , participle) ;
818+ self . warn_dead_code ( item. def_id , participle) ;
845819 } else {
846820 // Only continue if we didn't warn
847821 intravisit:: walk_item ( self , item) ;
@@ -865,8 +839,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
865839 . filter_map ( |variant| {
866840 if self . should_warn_about_variant ( & variant) {
867841 Some ( DeadVariant {
868- hir_id : variant. id ,
869- span : variant. span ,
842+ def_id : self . tcx . hir ( ) . local_def_id ( variant. id ) ,
870843 name : variant. ident . name ,
871844 level : self . tcx . lint_level_at_node ( lint:: builtin:: DEAD_CODE , variant. id ) . 0 ,
872845 } )
@@ -875,7 +848,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
875848 }
876849 } )
877850 . collect ( ) ;
878- self . warn_dead_fields_and_variants ( item_id, "constructed" , dead_variants)
851+ self . warn_dead_fields_and_variants ( item_id. expect_owner ( ) , "constructed" , dead_variants)
879852 }
880853
881854 fn visit_variant (
@@ -891,7 +864,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
891864
892865 fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
893866 if self . should_warn_about_foreign_item ( fi) {
894- self . warn_dead_code ( fi. hir_id ( ) , fi . span , fi . ident . name , "used" ) ;
867+ self . warn_dead_code ( fi. def_id , "used" ) ;
895868 }
896869 intravisit:: walk_foreign_item ( self , fi) ;
897870 }
@@ -911,8 +884,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
911884 . filter_map ( |field| {
912885 if self . should_warn_about_field ( & field) {
913886 Some ( DeadVariant {
914- hir_id : field. hir_id ,
915- span : field. span ,
887+ def_id : self . tcx . hir ( ) . local_def_id ( field. hir_id ) ,
916888 name : field. ident . name ,
917889 level : self
918890 . tcx
@@ -924,36 +896,20 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
924896 }
925897 } )
926898 . collect ( ) ;
927- self . warn_dead_fields_and_variants ( id , "read" , dead_fields)
899+ self . warn_dead_fields_and_variants ( self . tcx . hir ( ) . local_def_id ( id ) , "read" , dead_fields)
928900 }
929901
930902 fn visit_impl_item ( & mut self , impl_item : & ' tcx hir:: ImplItem < ' tcx > ) {
931903 match impl_item. kind {
932904 hir:: ImplItemKind :: Const ( _, body_id) => {
933905 if !self . symbol_is_live ( impl_item. def_id ) {
934- self . warn_dead_code (
935- impl_item. hir_id ( ) ,
936- impl_item. span ,
937- impl_item. ident . name ,
938- "used" ,
939- ) ;
906+ self . warn_dead_code ( impl_item. def_id , "used" ) ;
940907 }
941908 self . visit_nested_body ( body_id)
942909 }
943910 hir:: ImplItemKind :: Fn ( _, body_id) => {
944911 if !self . symbol_is_live ( impl_item. def_id ) {
945- // FIXME(66095): Because impl_item.span is annotated with things
946- // like expansion data, and ident.span isn't, we use the
947- // def_span method if it's part of a macro invocation
948- // (and thus has a source_callee set).
949- // We should probably annotate ident.span with the macro
950- // context, but that's a larger change.
951- let span = if impl_item. span . source_callee ( ) . is_some ( ) {
952- self . tcx . sess . source_map ( ) . guess_head_span ( impl_item. span )
953- } else {
954- impl_item. ident . span
955- } ;
956- self . warn_dead_code ( impl_item. hir_id ( ) , span, impl_item. ident . name , "used" ) ;
912+ self . warn_dead_code ( impl_item. def_id , "used" ) ;
957913 }
958914 self . visit_nested_body ( body_id)
959915 }
0 commit comments