@@ -508,6 +508,13 @@ fn find_live<'tcx>(
508508 symbol_visitor. live_symbols
509509}
510510
511+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
512+ enum ExtraNote {
513+ /// Use this to provide some examples in the diagnostic of potential other purposes for a value
514+ /// or field that is dead code
515+ OtherPurposeExamples ,
516+ }
517+
511518struct DeadVisitor < ' tcx > {
512519 tcx : TyCtxt < ' tcx > ,
513520 live_symbols : FxHashSet < hir:: HirId > ,
@@ -575,6 +582,7 @@ impl DeadVisitor<'tcx> {
575582 span : rustc_span:: Span ,
576583 name : Symbol ,
577584 participle : & str ,
585+ extra_note : Option < ExtraNote > ,
578586 ) {
579587 if !name. as_str ( ) . starts_with ( '_' ) {
580588 self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
@@ -585,19 +593,26 @@ impl DeadVisitor<'tcx> {
585593
586594 let mut diag =
587595 lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) ;
596+
588597 diag. multipart_suggestion (
589598 "if this is intentional, prefix it with an underscore" ,
590599 prefixed,
591600 Applicability :: MachineApplicable ,
592- )
593- . note ( & format ! (
594- "The leading underscore signals to the reader that while the {} may not be {}\n \
595- by any Rust code, it still serves some other purpose that isn't detected by rustc.\n \
596- (e.g. some values are used for their effect when dropped or used in FFI code\n \
597- exclusively through raw pointers)",
598- descr, participle,
599- ) ) ;
601+ ) ;
602+
603+ let mut note = format ! (
604+ "the leading underscore signals that this {} serves some other \
605+ purpose\n even if it isn't used in a way that we can detect.",
606+ descr,
607+ ) ;
608+ if matches ! ( extra_note, Some ( ExtraNote :: OtherPurposeExamples ) ) {
609+ note += " (e.g. for its effect\n when dropped or in foreign code)" ;
610+ }
611+
612+ diag. note ( & note) ;
613+
600614 // Force the note we added to the front, before any other subdiagnostics
615+ // added in lint.build(...)
601616 diag. children . rotate_right ( 1 ) ;
602617
603618 diag. emit ( )
@@ -646,7 +661,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
646661 hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
647662 _ => "used" ,
648663 } ;
649- self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle) ;
664+ self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle, None ) ;
650665 } else {
651666 // Only continue if we didn't warn
652667 intravisit:: walk_item ( self , item) ;
@@ -660,22 +675,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
660675 id : hir:: HirId ,
661676 ) {
662677 if self . should_warn_about_variant ( & variant) {
663- self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" ) ;
678+ self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" , None ) ;
664679 } else {
665680 intravisit:: walk_variant ( self , variant, g, id) ;
666681 }
667682 }
668683
669684 fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
670685 if self . should_warn_about_foreign_item ( fi) {
671- self . warn_dead_code ( fi. hir_id ( ) , fi. span , fi. ident . name , "used" ) ;
686+ self . warn_dead_code ( fi. hir_id ( ) , fi. span , fi. ident . name , "used" , None ) ;
672687 }
673688 intravisit:: walk_foreign_item ( self , fi) ;
674689 }
675690
676691 fn visit_field_def ( & mut self , field : & ' tcx hir:: FieldDef < ' tcx > ) {
677692 if self . should_warn_about_field ( & field) {
678- self . warn_dead_code ( field. hir_id , field. span , field. ident . name , "read" ) ;
693+ self . warn_dead_code (
694+ field. hir_id ,
695+ field. span ,
696+ field. ident . name ,
697+ "read" ,
698+ Some ( ExtraNote :: OtherPurposeExamples ) ,
699+ ) ;
679700 }
680701 intravisit:: walk_field_def ( self , field) ;
681702 }
@@ -689,6 +710,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
689710 impl_item. span ,
690711 impl_item. ident . name ,
691712 "used" ,
713+ None ,
692714 ) ;
693715 }
694716 self . visit_nested_body ( body_id)
@@ -706,7 +728,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
706728 } else {
707729 impl_item. ident . span
708730 } ;
709- self . warn_dead_code ( impl_item. hir_id ( ) , span, impl_item. ident . name , "used" ) ;
731+ self . warn_dead_code (
732+ impl_item. hir_id ( ) ,
733+ span,
734+ impl_item. ident . name ,
735+ "used" ,
736+ None ,
737+ ) ;
710738 }
711739 self . visit_nested_body ( body_id)
712740 }
0 commit comments