@@ -635,8 +635,7 @@ fn live_symbols_and_ignored_derived_traits<'tcx>(
635
635
}
636
636
637
637
struct DeadVariant {
638
- hir_id : hir:: HirId ,
639
- span : Span ,
638
+ def_id : LocalDefId ,
640
639
name : Symbol ,
641
640
level : lint:: Level ,
642
641
}
@@ -687,29 +686,39 @@ impl<'tcx> DeadVisitor<'tcx> {
687
686
688
687
fn warn_multiple_dead_codes (
689
688
& self ,
690
- dead_codes : & [ ( hir :: HirId , Span , Symbol ) ] ,
689
+ dead_codes : & [ LocalDefId ] ,
691
690
participle : & str ,
692
- parent_hir_id : Option < hir :: HirId > ,
691
+ parent_item : Option < LocalDefId > ,
693
692
) {
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 (
698
708
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) ,
703
711
|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 ( ) ) ;
706
713
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[ ..] {
709
715
_ if span_len > 6 => String :: new ( ) ,
710
716
[ name] => format ! ( "`{name}` " ) ,
711
717
[ 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
+ )
713
722
}
714
723
[ ] => unreachable ! ( ) ,
715
724
} ;
@@ -719,25 +728,17 @@ impl<'tcx> DeadVisitor<'tcx> {
719
728
s = pluralize!( span_len) ,
720
729
are = pluralize!( "is" , span_len) ,
721
730
) ) ;
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 ( ) ) ;
729
734
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) ) ,
735
737
) ;
736
738
}
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) {
741
742
let traits_str = ign_traits
742
743
. iter ( )
743
744
. map ( |( trait_id, _) | format ! ( "`{}`" , self . tcx. item_name( * trait_id) ) )
@@ -758,15 +759,15 @@ impl<'tcx> DeadVisitor<'tcx> {
758
759
) ;
759
760
err. note ( & msg) ;
760
761
}
761
- err. emit ( ) ;
762
- } ,
762
+ err. emit ( ) ;
763
+ } ,
763
764
) ;
764
765
}
765
766
}
766
767
767
768
fn warn_dead_fields_and_variants (
768
769
& self ,
769
- hir_id : hir :: HirId ,
770
+ def_id : LocalDefId ,
770
771
participle : & str ,
771
772
dead_codes : Vec < DeadVariant > ,
772
773
) {
@@ -781,23 +782,18 @@ impl<'tcx> DeadVisitor<'tcx> {
781
782
dead_codes. sort_by_key ( |v| v. level ) ;
782
783
for ( _, group) in & dead_codes. into_iter ( ) . group_by ( |v| v. level ) {
783
784
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 < _ > > ( ) ,
787
786
participle,
788
- Some ( hir_id ) ,
787
+ Some ( def_id ) ,
789
788
) ;
790
789
}
791
790
}
792
791
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 ) ;
801
797
}
802
798
}
803
799
@@ -815,33 +811,11 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
815
811
fn visit_item ( & mut self , item : & ' tcx hir:: Item < ' tcx > ) {
816
812
if self . should_warn_about_item ( item) {
817
813
// 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
- } ;
840
814
let participle = match item. kind {
841
815
hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
842
816
_ => "used" ,
843
817
} ;
844
- self . warn_dead_code ( item. hir_id ( ) , span , item . ident . name , participle) ;
818
+ self . warn_dead_code ( item. def_id , participle) ;
845
819
} else {
846
820
// Only continue if we didn't warn
847
821
intravisit:: walk_item ( self , item) ;
@@ -865,8 +839,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
865
839
. filter_map ( |variant| {
866
840
if self . should_warn_about_variant ( & variant) {
867
841
Some ( DeadVariant {
868
- hir_id : variant. id ,
869
- span : variant. span ,
842
+ def_id : self . tcx . hir ( ) . local_def_id ( variant. id ) ,
870
843
name : variant. ident . name ,
871
844
level : self . tcx . lint_level_at_node ( lint:: builtin:: DEAD_CODE , variant. id ) . 0 ,
872
845
} )
@@ -875,7 +848,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
875
848
}
876
849
} )
877
850
. 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)
879
852
}
880
853
881
854
fn visit_variant (
@@ -891,7 +864,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
891
864
892
865
fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
893
866
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" ) ;
895
868
}
896
869
intravisit:: walk_foreign_item ( self , fi) ;
897
870
}
@@ -911,8 +884,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
911
884
. filter_map ( |field| {
912
885
if self . should_warn_about_field ( & field) {
913
886
Some ( DeadVariant {
914
- hir_id : field. hir_id ,
915
- span : field. span ,
887
+ def_id : self . tcx . hir ( ) . local_def_id ( field. hir_id ) ,
916
888
name : field. ident . name ,
917
889
level : self
918
890
. tcx
@@ -924,36 +896,20 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
924
896
}
925
897
} )
926
898
. 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)
928
900
}
929
901
930
902
fn visit_impl_item ( & mut self , impl_item : & ' tcx hir:: ImplItem < ' tcx > ) {
931
903
match impl_item. kind {
932
904
hir:: ImplItemKind :: Const ( _, body_id) => {
933
905
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" ) ;
940
907
}
941
908
self . visit_nested_body ( body_id)
942
909
}
943
910
hir:: ImplItemKind :: Fn ( _, body_id) => {
944
911
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" ) ;
957
913
}
958
914
self . visit_nested_body ( body_id)
959
915
}
0 commit comments