|
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};
|
@@ -577,7 +578,26 @@ impl DeadVisitor<'tcx> {
|
577 | 578 | self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
|
578 | 579 | let def_id = self.tcx.hir().local_def_id(id);
|
579 | 580 | let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
|
580 |
| - lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit() |
| 581 | + |
| 582 | + let prefixed = vec![(span, format!("_{}", name))]; |
| 583 | + |
| 584 | + let mut diag = |
| 585 | + lint.build(&format!("{} is never {}: `{}`", descr, participle, name)); |
| 586 | + diag.multipart_suggestion( |
| 587 | + "if this is intentional, prefix it with an underscore", |
| 588 | + prefixed, |
| 589 | + Applicability::MachineApplicable, |
| 590 | + ) |
| 591 | + .note(&format!( |
| 592 | + "the leading underscore helps signal to the reader that the {} may still serve\n\ |
| 593 | + a purpose even if it isn't used in a way that we can detect (e.g. the {}\nis \ |
| 594 | + only used through FFI or used only for its effect when dropped)", |
| 595 | + descr, descr, |
| 596 | + )); |
| 597 | + // Force the note we added to the front, before any other subdiagnostics |
| 598 | + diag.children.rotate_right(1); |
| 599 | + |
| 600 | + diag.emit() |
581 | 601 | });
|
582 | 602 | }
|
583 | 603 | }
|
|
0 commit comments