@@ -723,6 +723,12 @@ impl<'tcx> DeadVisitor<'tcx> {
723
723
ShouldWarnAboutField :: Yes ( is_positional)
724
724
}
725
725
726
+ // # Panics
727
+ // All `dead_codes` must have the same lint level, otherwise we will intentionally ICE.
728
+ // This is because we emit a multi-spanned lint using the lint level of the `dead_codes`'s
729
+ // first local def id.
730
+ // Prefer calling `Self.warn_dead_code` or `Self.warn_dead_code_grouped_by_lint_level`
731
+ // since those methods group by lint level before calling this method.
726
732
fn warn_multiple_dead_codes (
727
733
& self ,
728
734
dead_codes : & [ LocalDefId ] ,
@@ -734,6 +740,15 @@ impl<'tcx> DeadVisitor<'tcx> {
734
740
return ;
735
741
} ;
736
742
let tcx = self . tcx ;
743
+
744
+ let first_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( first_id) ;
745
+ let first_lint_level = tcx. lint_level_at_node ( lint:: builtin:: DEAD_CODE , first_hir_id) . 0 ;
746
+ assert ! ( dead_codes. iter( ) . skip( 1 ) . all( |id| {
747
+ let hir_id = tcx. hir( ) . local_def_id_to_hir_id( * id) ;
748
+ let level = tcx. lint_level_at_node( lint:: builtin:: DEAD_CODE , hir_id) . 0 ;
749
+ level == first_lint_level
750
+ } ) ) ;
751
+
737
752
let names: Vec < _ > =
738
753
dead_codes. iter ( ) . map ( |& def_id| tcx. item_name ( def_id. to_def_id ( ) ) ) . collect ( ) ;
739
754
let spans: Vec < _ > = dead_codes
@@ -814,18 +829,9 @@ impl<'tcx> DeadVisitor<'tcx> {
814
829
}
815
830
} ;
816
831
817
- // FIXME: Remove this before landing the PR.
818
- // Just keeping it around so that I remember how to get the expectation id.
819
- // for id in &dead_codes[1..] {
820
- // let hir = self.tcx.hir().local_def_id_to_hir_id(*id);
821
- // let lint_level = self.tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir).0;
822
- // if let Some(expectation_id) = lint_level.get_expectation_id() {
823
- // self.tcx.sess.diagnostic().insert_fulfilled_expectation(expectation_id);
824
- // }
825
- // }
826
832
self . tcx . emit_spanned_lint (
827
833
lint,
828
- tcx . hir ( ) . local_def_id_to_hir_id ( first_id ) ,
834
+ first_hir_id ,
829
835
MultiSpan :: from_spans ( spans) ,
830
836
diag,
831
837
) ;
@@ -903,14 +909,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
903
909
if let hir:: ItemKind :: Impl ( impl_item) = tcx. hir ( ) . item ( item) . kind {
904
910
let mut dead_items = Vec :: new ( ) ;
905
911
for item in impl_item. items {
906
- let did = item. id . owner_id . def_id ;
907
- if !visitor. is_live_code ( did ) {
912
+ let def_id = item. id . owner_id . def_id ;
913
+ if !visitor. is_live_code ( def_id ) {
908
914
let name = tcx. item_name ( def_id. to_def_id ( ) ) ;
909
- let hir = tcx. hir ( ) . local_def_id_to_hir_id ( did ) ;
915
+ let hir = tcx. hir ( ) . local_def_id_to_hir_id ( def_id ) ;
910
916
let level = tcx. lint_level_at_node ( lint:: builtin:: DEAD_CODE , hir) . 0 ;
911
917
912
918
dead_items. push ( DeadVariant {
913
- def_id : did ,
919
+ def_id,
914
920
name,
915
921
level,
916
922
} )
0 commit comments