3
3
// from live codes are live, and everything else is dead.
4
4
5
5
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
6
+ use rustc_errors:: Applicability ;
6
7
use rustc_hir as hir;
7
8
use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
8
9
use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
@@ -15,7 +16,7 @@ use rustc_middle::middle::privacy;
15
16
use rustc_middle:: ty:: { self , DefIdTree , TyCtxt } ;
16
17
use rustc_session:: lint;
17
18
18
- use rustc_span:: symbol:: { sym, Symbol } ;
19
+ use rustc_span:: symbol:: { sym, Ident , Symbol } ;
19
20
20
21
// Any local node that may call something in its body block should be
21
22
// explored. For example, if it's a live Node::Item that is a
@@ -507,6 +508,13 @@ fn find_live<'tcx>(
507
508
symbol_visitor. live_symbols
508
509
}
509
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
+
510
518
struct DeadVisitor < ' tcx > {
511
519
tcx : TyCtxt < ' tcx > ,
512
520
live_symbols : FxHashSet < hir:: HirId > ,
@@ -572,14 +580,42 @@ impl DeadVisitor<'tcx> {
572
580
& mut self ,
573
581
id : hir:: HirId ,
574
582
span : rustc_span:: Span ,
575
- name : Symbol ,
583
+ name : Ident ,
576
584
participle : & str ,
585
+ extra_note : Option < ExtraNote > ,
577
586
) {
578
587
if !name. as_str ( ) . starts_with ( '_' ) {
579
588
self . tcx . struct_span_lint_hir ( lint:: builtin:: DEAD_CODE , id, span, |lint| {
580
589
let def_id = self . tcx . hir ( ) . local_def_id ( id) ;
581
590
let descr = self . tcx . def_kind ( def_id) . descr ( def_id. to_def_id ( ) ) ;
582
- lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) . emit ( )
591
+
592
+ let prefixed = vec ! [ ( name. span, format!( "_{}" , name) ) ] ;
593
+
594
+ let mut diag =
595
+ lint. build ( & format ! ( "{} is never {}: `{}`" , descr, participle, name) ) ;
596
+
597
+ diag. multipart_suggestion (
598
+ "if this is intentional, prefix it with an underscore" ,
599
+ prefixed,
600
+ Applicability :: MachineApplicable ,
601
+ ) ;
602
+
603
+ let mut note = format ! (
604
+ "the leading underscore signals that this {} serves some other \
605
+ purpose 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 when dropped or in foreign code)" ;
610
+ }
611
+
612
+ diag. note ( & note) ;
613
+
614
+ // Force the note we added to the front, before any other subdiagnostics
615
+ // added in lint.build(...)
616
+ diag. children . rotate_right ( 1 ) ;
617
+
618
+ diag. emit ( )
583
619
} ) ;
584
620
}
585
621
}
@@ -625,7 +661,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
625
661
hir:: ItemKind :: Struct ( ..) => "constructed" , // Issue #52325
626
662
_ => "used" ,
627
663
} ;
628
- self . warn_dead_code ( item. hir_id ( ) , span, item. ident . name , participle) ;
664
+ self . warn_dead_code ( item. hir_id ( ) , span, item. ident , participle, None ) ;
629
665
} else {
630
666
// Only continue if we didn't warn
631
667
intravisit:: walk_item ( self , item) ;
@@ -639,22 +675,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
639
675
id : hir:: HirId ,
640
676
) {
641
677
if self . should_warn_about_variant ( & variant) {
642
- self . warn_dead_code ( variant. id , variant. span , variant. ident . name , "constructed" ) ;
678
+ self . warn_dead_code ( variant. id , variant. span , variant. ident , "constructed" , None ) ;
643
679
} else {
644
680
intravisit:: walk_variant ( self , variant, g, id) ;
645
681
}
646
682
}
647
683
648
684
fn visit_foreign_item ( & mut self , fi : & ' tcx hir:: ForeignItem < ' tcx > ) {
649
685
if self . should_warn_about_foreign_item ( fi) {
650
- 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 , "used" , None ) ;
651
687
}
652
688
intravisit:: walk_foreign_item ( self , fi) ;
653
689
}
654
690
655
691
fn visit_field_def ( & mut self , field : & ' tcx hir:: FieldDef < ' tcx > ) {
656
692
if self . should_warn_about_field ( & field) {
657
- 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 ,
697
+ "read" ,
698
+ Some ( ExtraNote :: OtherPurposeExamples ) ,
699
+ ) ;
658
700
}
659
701
intravisit:: walk_field_def ( self , field) ;
660
702
}
@@ -666,8 +708,9 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
666
708
self . warn_dead_code (
667
709
impl_item. hir_id ( ) ,
668
710
impl_item. span ,
669
- impl_item. ident . name ,
711
+ impl_item. ident ,
670
712
"used" ,
713
+ None ,
671
714
) ;
672
715
}
673
716
self . visit_nested_body ( body_id)
@@ -685,7 +728,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
685
728
} else {
686
729
impl_item. ident . span
687
730
} ;
688
- self . warn_dead_code ( impl_item. hir_id ( ) , span, impl_item. ident . name , "used" ) ;
731
+ self . warn_dead_code ( impl_item. hir_id ( ) , span, impl_item. ident , "used" , None ) ;
689
732
}
690
733
self . visit_nested_body ( body_id)
691
734
}
0 commit comments