@@ -506,6 +506,13 @@ fn find_live<'tcx>(
506
506
symbol_visitor. live_symbols
507
507
}
508
508
509
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
510
+ enum ExtraNote {
511
+ /// Use this to provide some examples in the diagnostic of potential other purposes for a value
512
+ /// or field that is dead code
513
+ OtherPurposeExamples ,
514
+ }
515
+
509
516
struct DeadVisitor < ' tcx > {
510
517
tcx : TyCtxt < ' tcx > ,
511
518
live_symbols : FxHashSet < hir:: HirId > ,
@@ -573,6 +580,7 @@ impl DeadVisitor<'tcx> {
573
580
span : rustc_span:: Span ,
574
581
name : Symbol ,
575
582
participle : & str ,
583
+ extra_note : Option < ExtraNote > ,
576
584
) {
577
585
if !name. as_str ( ) . starts_with ( '_' ) {
578
586
self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
@@ -583,19 +591,26 @@ impl DeadVisitor<'tcx> {
583
591
584
592
let mut diag =
585
593
lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) ;
594
+
586
595
diag. multipart_suggestion (
587
596
"if this is intentional, prefix it with an underscore" ,
588
597
prefixed,
589
598
Applicability :: MachineApplicable ,
590
- )
591
- . note ( & format ! (
592
- "The leading underscore signals to the reader that while the {} may not be {}\n \
593
- by any Rust code, it still serves some other purpose that isn't detected by rustc.\n \
594
- (e.g. some values are used for their effect when dropped or used in FFI code\n \
595
- exclusively through raw pointers)",
596
- descr, participle,
597
- ) ) ;
599
+ ) ;
600
+
601
+ let mut note = format ! (
602
+ "the leading underscore signals that this {} serves some other \
603
+ purpose\n even if it isn't used in a way that we can detect.",
604
+ descr,
605
+ ) ;
606
+ if matches ! ( extra_note, Some ( ExtraNote :: OtherPurposeExamples ) ) {
607
+ note += " (e.g. for its effect\n when dropped or in foreign code)" ;
608
+ }
609
+
610
+ diag. note ( & note) ;
611
+
598
612
// Force the note we added to the front, before any other subdiagnostics
613
+ // added in lint.build(...)
599
614
diag. children . rotate_right ( 1 ) ;
600
615
601
616
diag. emit ( )
@@ -644,7 +659,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
644
659
hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
645
660
_ => "used" ,
646
661
} ;
647
- self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle) ;
662
+ self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle, None ) ;
648
663
} else {
649
664
// Only continue if we didn't warn
650
665
intravisit:: walk_item ( self , item) ;
@@ -658,22 +673,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
658
673
id : hir:: HirId ,
659
674
) {
660
675
if self . should_warn_about_variant ( & variant) {
661
- self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" ) ;
676
+ self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" , None ) ;
662
677
} else {
663
678
intravisit:: walk_variant ( self , variant, g, id) ;
664
679
}
665
680
}
666
681
667
682
fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
668
683
if self . should_warn_about_foreign_item ( fi) {
669
- self . warn_dead_code ( fi. hir_id ( ) , fi. span , fi. ident . name , "used" ) ;
684
+ self . warn_dead_code ( fi. hir_id ( ) , fi. span , fi. ident . name , "used" , None ) ;
670
685
}
671
686
intravisit:: walk_foreign_item ( self , fi) ;
672
687
}
673
688
674
689
fn visit_field_def ( & mut self , field : & ' tcx hir:: FieldDef < ' tcx > ) {
675
690
if self . should_warn_about_field ( & field) {
676
- self . warn_dead_code ( field. hir_id , field. span , field. ident . name , "read" ) ;
691
+ self . warn_dead_code (
692
+ field. hir_id ,
693
+ field. span ,
694
+ field. ident . name ,
695
+ "read" ,
696
+ Some ( ExtraNote :: OtherPurposeExamples ) ,
697
+ ) ;
677
698
}
678
699
intravisit:: walk_field_def ( self , field) ;
679
700
}
@@ -687,6 +708,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
687
708
impl_item. span ,
688
709
impl_item. ident . name ,
689
710
"used" ,
711
+ None ,
690
712
) ;
691
713
}
692
714
self . visit_nested_body ( body_id)
@@ -704,7 +726,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
704
726
} else {
705
727
impl_item. ident . span
706
728
} ;
707
- self . warn_dead_code ( impl_item. hir_id ( ) , span, impl_item. ident . name , "used" ) ;
729
+ self . warn_dead_code (
730
+ impl_item. hir_id ( ) ,
731
+ span,
732
+ impl_item. ident . name ,
733
+ "used" ,
734
+ None ,
735
+ ) ;
708
736
}
709
737
self . visit_nested_body ( body_id)
710
738
}
0 commit comments