Skip to content

Improve diagnostic for when field is never read #83004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Prev Previous commit
Next Next commit
Added suggestion and note for when a field is never used
sunjay committed Jun 10, 2021
commit 9b7d139b79825e31bb196cebd863f81912c3320d
22 changes: 21 additions & 1 deletion compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
// from live codes are live, and everything else is dead.

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId;
@@ -588,7 +589,26 @@ impl DeadVisitor<'tcx> {
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
let def_id = self.tcx.hir().local_def_id(id);
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
lint.build(&format!("{} is never {}: `{}`", descr, participle, name)).emit()

let prefixed = vec![(span, format!("_{}", name))];

let mut diag =
lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
diag.multipart_suggestion(
"if this is intentional, prefix it with an underscore",
prefixed,
Applicability::MachineApplicable,
)
.note(&format!(
"the leading underscore helps signal to the reader that the {} may still serve\n\
a purpose even if it isn't used in a way that we can detect (e.g. the {}\nis \
only used through FFI or used only for its effect when dropped)",
descr, descr,
));
// Force the note we added to the front, before any other subdiagnostics
diag.children.rotate_right(1);

diag.emit()
});
}
}