@@ -517,6 +517,13 @@ fn find_live<'tcx>(
517
517
symbol_visitor. live_symbols
518
518
}
519
519
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
+
520
527
struct DeadVisitor < ' tcx > {
521
528
tcx : TyCtxt < ' tcx > ,
522
529
live_symbols : FxHashSet < hir:: HirId > ,
@@ -584,6 +591,7 @@ impl DeadVisitor<'tcx> {
584
591
span : rustc_span:: Span ,
585
592
name : Symbol ,
586
593
participle : & str ,
594
+ extra_note : Option < ExtraNote > ,
587
595
) {
588
596
if !name. as_str ( ) . starts_with ( '_' ) {
589
597
self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
@@ -594,19 +602,26 @@ impl DeadVisitor<'tcx> {
594
602
595
603
let mut diag =
596
604
lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) ;
605
+
597
606
diag. multipart_suggestion (
598
607
"if this is intentional, prefix it with an underscore" ,
599
608
prefixed,
600
609
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
+
609
623
// Force the note we added to the front, before any other subdiagnostics
624
+ // added in lint.build(...)
610
625
diag. children . rotate_right ( 1 ) ;
611
626
612
627
diag. emit ( )
@@ -655,7 +670,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
655
670
hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
656
671
_ => "used" ,
657
672
} ;
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 ) ;
659
674
} else {
660
675
// Only continue if we didn't warn
661
676
intravisit:: walk_item ( self , item) ;
@@ -669,22 +684,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
669
684
id : hir:: HirId ,
670
685
) {
671
686
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 ) ;
673
688
} else {
674
689
intravisit:: walk_variant ( self , variant, g, id) ;
675
690
}
676
691
}
677
692
678
693
fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
679
694
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 ) ;
681
696
}
682
697
intravisit:: walk_foreign_item ( self , fi) ;
683
698
}
684
699
685
700
fn visit_field_def ( & mut self , field : & ' tcx hir:: FieldDef < ' tcx > ) {
686
701
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
+ ) ;
688
709
}
689
710
intravisit:: walk_field_def ( self , field) ;
690
711
}
@@ -698,6 +719,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
698
719
impl_item. span ,
699
720
impl_item. ident . name ,
700
721
"used" ,
722
+ None ,
701
723
) ;
702
724
}
703
725
self . visit_nested_body ( body_id)
@@ -715,7 +737,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
715
737
} else {
716
738
impl_item. ident . span
717
739
} ;
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
+ ) ;
719
747
}
720
748
self . visit_nested_body ( body_id)
721
749
}
0 commit comments