Skip to content

Commit 321aace

Browse files
committed
Added suggestion and note for when a field is never used
1 parent 30fc601 commit 321aace

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

compiler/rustc_passes/src/dead.rs

+21-1
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};
@@ -577,7 +578,26 @@ impl DeadVisitor<'tcx> {
577578
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
578579
let def_id = self.tcx.hir().local_def_id(id);
579580
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()
581601
});
582602
}
583603
}

0 commit comments

Comments
 (0)