Skip to content

Commit 9b7d139

Browse files
committed
Added suggestion and note for when a field is never used
1 parent 5a8bd7d commit 9b7d139

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

0 commit comments

Comments
 (0)