@@ -508,6 +508,13 @@ fn find_live<'tcx>(
508
508
symbol_visitor. live_symbols
509
509
}
510
510
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
+
511
518
struct DeadVisitor < ' tcx > {
512
519
tcx : TyCtxt < ' tcx > ,
513
520
live_symbols : FxHashSet < hir:: HirId > ,
@@ -575,6 +582,7 @@ impl DeadVisitor<'tcx> {
575
582
span : rustc_span:: Span ,
576
583
name : Symbol ,
577
584
participle : & str ,
585
+ extra_note : Option < ExtraNote > ,
578
586
) {
579
587
if !name. as_str ( ) . starts_with ( '_' ) {
580
588
self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
@@ -585,19 +593,26 @@ impl DeadVisitor<'tcx> {
585
593
586
594
let mut diag =
587
595
lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) ;
596
+
588
597
diag. multipart_suggestion (
589
598
"if this is intentional, prefix it with an underscore" ,
590
599
prefixed,
591
600
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
+
600
614
// Force the note we added to the front, before any other subdiagnostics
615
+ // added in lint.build(...)
601
616
diag. children . rotate_right ( 1 ) ;
602
617
603
618
diag. emit ( )
@@ -646,7 +661,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
646
661
hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
647
662
_ => "used" ,
648
663
} ;
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 ) ;
650
665
} else {
651
666
// Only continue if we didn't warn
652
667
intravisit:: walk_item ( self , item) ;
@@ -660,22 +675,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
660
675
id : hir:: HirId ,
661
676
) {
662
677
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 ) ;
664
679
} else {
665
680
intravisit:: walk_variant ( self , variant, g, id) ;
666
681
}
667
682
}
668
683
669
684
fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
670
685
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 ) ;
672
687
}
673
688
intravisit:: walk_foreign_item ( self , fi) ;
674
689
}
675
690
676
691
fn visit_field_def ( & mut self , field : & ' tcx hir:: FieldDef < ' tcx > ) {
677
692
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
+ ) ;
679
700
}
680
701
intravisit:: walk_field_def ( self , field) ;
681
702
}
@@ -689,6 +710,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
689
710
impl_item. span ,
690
711
impl_item. ident . name ,
691
712
"used" ,
713
+ None ,
692
714
) ;
693
715
}
694
716
self . visit_nested_body ( body_id)
@@ -706,7 +728,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
706
728
} else {
707
729
impl_item. ident . span
708
730
} ;
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
+ ) ;
710
738
}
711
739
self . visit_nested_body ( body_id)
712
740
}
0 commit comments