@@ -517,6 +517,13 @@ fn find_live<'tcx>(
517517 symbol_visitor. live_symbols
518518}
519519
520+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
521+ enum ExtraNote {
522+ /// Use this to provide some examples in the diagnostic of potential other purposes for a value
523+ /// or field that is dead code
524+ OtherPurposeExamples ,
525+ }
526+
520527struct DeadVisitor < ' tcx > {
521528 tcx : TyCtxt < ' tcx > ,
522529 live_symbols : FxHashSet < hir:: HirId > ,
@@ -584,6 +591,7 @@ impl DeadVisitor<'tcx> {
584591 span : rustc_span:: Span ,
585592 name : Symbol ,
586593 participle : & str ,
594+ extra_note : Option < ExtraNote > ,
587595 ) {
588596 if !name. as_str ( ) . starts_with ( '_' ) {
589597 self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
@@ -594,19 +602,26 @@ impl DeadVisitor<'tcx> {
594602
595603 let mut diag =
596604 lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) ;
605+
597606 diag. multipart_suggestion (
598607 "if this is intentional, prefix it with an underscore" ,
599608 prefixed,
600609 Applicability :: MachineApplicable ,
601- )
602- . note ( & format ! (
603- "The leading underscore signals to the reader that while the {} may not be {}\n \
604- by any Rust code, it still serves some other purpose that isn't detected by rustc.\n \
605- (e.g. some values are used for their effect when dropped or used in FFI code\n \
606- exclusively through raw pointers)",
607- descr, participle,
608- ) ) ;
610+ ) ;
611+
612+ let mut note = format ! (
613+ "the leading underscore signals that this {} serves some other \
614+ purpose\n even if it isn't used in a way that we can detect.",
615+ descr,
616+ ) ;
617+ if matches ! ( extra_note, Some ( ExtraNote :: OtherPurposeExamples ) ) {
618+ note += " (e.g. for its effect\n when dropped or in foreign code)" ;
619+ }
620+
621+ diag. note ( & note) ;
622+
609623 // Force the note we added to the front, before any other subdiagnostics
624+ // added in lint.build(...)
610625 diag. children . rotate_right ( 1 ) ;
611626
612627 diag. emit ( )
@@ -655,7 +670,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
655670 hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
656671 _ => "used" ,
657672 } ;
658- self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle) ;
673+ self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle, None ) ;
659674 } else {
660675 // Only continue if we didn't warn
661676 intravisit:: walk_item ( self , item) ;
@@ -669,22 +684,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
669684 id : hir:: HirId ,
670685 ) {
671686 if self . should_warn_about_variant ( & variant) {
672- self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" ) ;
687+ self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" , None ) ;
673688 } else {
674689 intravisit:: walk_variant ( self , variant, g, id) ;
675690 }
676691 }
677692
678693 fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
679694 if self . should_warn_about_foreign_item ( fi) {
680- self . warn_dead_code ( fi. hir_id ( ) , fi. span , fi. ident . name , "used" ) ;
695+ self . warn_dead_code ( fi. hir_id ( ) , fi. span , fi. ident . name , "used" , None ) ;
681696 }
682697 intravisit:: walk_foreign_item ( self , fi) ;
683698 }
684699
685700 fn visit_field_def ( & mut self , field : & ' tcx hir:: FieldDef < ' tcx > ) {
686701 if self . should_warn_about_field ( & field) {
687- self . warn_dead_code ( field. hir_id , field. span , field. ident . name , "read" ) ;
702+ self . warn_dead_code (
703+ field. hir_id ,
704+ field. span ,
705+ field. ident . name ,
706+ "read" ,
707+ Some ( ExtraNote :: OtherPurposeExamples ) ,
708+ ) ;
688709 }
689710 intravisit:: walk_field_def ( self , field) ;
690711 }
@@ -698,6 +719,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
698719 impl_item. span ,
699720 impl_item. ident . name ,
700721 "used" ,
722+ None ,
701723 ) ;
702724 }
703725 self . visit_nested_body ( body_id)
@@ -715,7 +737,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
715737 } else {
716738 impl_item. ident . span
717739 } ;
718- self . warn_dead_code ( impl_item. hir_id ( ) , span, impl_item. ident . name , "used" ) ;
740+ self . warn_dead_code (
741+ impl_item. hir_id ( ) ,
742+ span,
743+ impl_item. ident . name ,
744+ "used" ,
745+ None ,
746+ ) ;
719747 }
720748 self . visit_nested_body ( body_id)
721749 }
0 commit comments