@@ -723,6 +723,12 @@ impl<'tcx> DeadVisitor<'tcx> {
723723 ShouldWarnAboutField :: Yes ( is_positional)
724724 }
725725
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.
726732 fn warn_multiple_dead_codes (
727733 & self ,
728734 dead_codes : & [ LocalDefId ] ,
@@ -734,6 +740,15 @@ impl<'tcx> DeadVisitor<'tcx> {
734740 return ;
735741 } ;
736742 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+
737752 let names: Vec < _ > =
738753 dead_codes. iter ( ) . map ( |& def_id| tcx. item_name ( def_id. to_def_id ( ) ) ) . collect ( ) ;
739754 let spans: Vec < _ > = dead_codes
@@ -814,18 +829,9 @@ impl<'tcx> DeadVisitor<'tcx> {
814829 }
815830 } ;
816831
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- // }
826832 self . tcx . emit_spanned_lint (
827833 lint,
828- tcx . hir ( ) . local_def_id_to_hir_id ( first_id ) ,
834+ first_hir_id ,
829835 MultiSpan :: from_spans ( spans) ,
830836 diag,
831837 ) ;
@@ -903,14 +909,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
903909 if let hir:: ItemKind :: Impl ( impl_item) = tcx. hir ( ) . item ( item) . kind {
904910 let mut dead_items = Vec :: new ( ) ;
905911 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 ) {
908914 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 ) ;
910916 let level = tcx. lint_level_at_node ( lint:: builtin:: DEAD_CODE , hir) . 0 ;
911917
912918 dead_items. push ( DeadVariant {
913- def_id : did ,
919+ def_id,
914920 name,
915921 level,
916922 } )
0 commit comments