Skip to content

Commit 67f228e

Browse files
committed
Added suggestion and note for when a field is never used
1 parent 450d121 commit 67f228e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// from live codes are live, and everything else is dead.
44

55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6+
use rustc_errors::Applicability;
67
use rustc_hir as hir;
78
use rustc_hir::def::{CtorOf, DefKind, Res};
89
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
@@ -579,7 +580,26 @@ impl DeadVisitor<'tcx> {
579580
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
580581
let def_id = self.tcx.hir().local_def_id(id);
581582
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
582-
lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit()
583+
584+
let prefixed = vec![(span, format!("_{}", name))];
585+
586+
let mut diag =
587+
lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
588+
diag.multipart_suggestion(
589+
"if this is intentional, prefix it with an underscore",
590+
prefixed,
591+
Applicability::MachineApplicable,
592+
)
593+
.note(&format!(
594+
"the leading underscore helps signal to the reader that the {} may still serve\n\
595+
a purpose even if it isn't used in a way that we can detect (e.g. the {}\nis \
596+
only used through FFI or used only for its effect when dropped)",
597+
descr, descr,
598+
));
599+
// Force the note we added to the front, before any other subdiagnostics
600+
diag.children.rotate_right(1);
601+
602+
diag.emit()
583603
});
584604
}
585605
}

0 commit comments

Comments
 (0)