Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
30fc601
Tests for field is never read diagnostic
sunjay Mar 11, 2021
321aace
Added suggestion and note for when a field is never used
sunjay Mar 12, 2021
7faaf39
Updating test stderr files
sunjay Mar 12, 2021
789186d
Trying out a new message that works a little better for values *and* …
sunjay Mar 12, 2021
2acd8eb
New shorter diagnostic note that is different for items versus fields
sunjay Mar 13, 2021
3e34eb8
Putting help message only under the identifier that needs to be prefixed
sunjay Mar 25, 2021
53a1105
On stable, suggest removing `#![feature]` for features that have been…
jyn514 Mar 31, 2021
37a5b51
implement TrustedRandomAccess for Take iterator adapter
the8472 Apr 8, 2021
03900e4
move core::hint::black_box under its own feature gate
RalfJung Apr 15, 2021
259a368
fix name resolution for param defaults
lcnr Apr 18, 2021
7cb1dcd
loosen ordering restricts for `const_generics_defaults`
lcnr Apr 18, 2021
312b4fd
improve wf check for const param defaults
lcnr Apr 18, 2021
d3e0d2f
supply substs to anon consts in defaults
lcnr Apr 18, 2021
5d9eeff
Ensure TLS destructors run before thread joins in SGX
mzohreva Apr 21, 2021
9cabbd0
Move `sys_common::poison` to `sync::poison`
CDirkx Apr 21, 2021
3794fc9
Clean up .gitignore
CDirkx Apr 20, 2021
c0d05d2
Remove "Version control"
CDirkx Apr 20, 2021
bfbf4ce
doc/platform-support: clarify UEFI support
dvdhrm Apr 22, 2021
a03cba3
Prevent control, shift and alt keys to make search input lose focus
GuillaumeGomez Apr 22, 2021
c247055
Get rid of "[+] show undocumented items" toggle on numeric From impls
notriddle Apr 22, 2021
a07bf2e
Fix ICE if original_span(fn_sig) returns a span not in body sourcefile
richkadel Apr 22, 2021
e1d9b3c
Take ItemType instead of TypeKind in record_extern_fqn
jyn514 Apr 23, 2021
3170536
Use ItemType in cache
jyn514 Apr 23, 2021
2df886d
Remove `TypeKind`
jyn514 Apr 23, 2021
26f2705
Remove unused `impl Clean<ItemType> for DefKind`
jyn514 Apr 23, 2021
a344282
Don't repeat `hir::def::DefKind` so much
jyn514 Apr 23, 2021
6c066ab
Fix macro bug in `impl From<DefKind> for ItemType`
jyn514 Apr 23, 2021
c1436c6
Rollup merge of #83004 - sunjay:field-never-read-issue-81658, r=pnkfelix
Dylan-DPC Apr 23, 2021
14bbf69
Rollup merge of #83722 - jyn514:stable-help, r=estebank
Dylan-DPC Apr 23, 2021
18523ee
Rollup merge of #83990 - the8472:take-trusted-len, r=dtolnay
Dylan-DPC Apr 23, 2021
6ed8462
Rollup merge of #84216 - RalfJung:black-box, r=Mark-Simulacrum
Dylan-DPC Apr 23, 2021
9cf575a
Rollup merge of #84299 - lcnr:const-generics-defaults-name-res, r=varkor
Dylan-DPC Apr 23, 2021
096bb93
Rollup merge of #84374 - CDirkx:gitignore, r=Mark-Simulacrum
Dylan-DPC Apr 23, 2021
4a63cad
Rollup merge of #84387 - CDirkx:poison, r=m-ou-se
Dylan-DPC Apr 23, 2021
51f90fb
Rollup merge of #84409 - mzohreva:mz/tls-dtors-before-join, r=dtolnay
Dylan-DPC Apr 23, 2021
35cda5b
Rollup merge of #84430 - dvdhrm:rw/uefidoc, r=Amanieu
Dylan-DPC Apr 23, 2021
1ab01e8
Rollup merge of #84433 - GuillaumeGomez:search-input-blur, r=jsha
Dylan-DPC Apr 23, 2021
156294b
Rollup merge of #84444 - notriddle:num-docs-from-undocumented-items-t…
Dylan-DPC Apr 23, 2021
9bad270
Rollup merge of #84456 - richkadel:issue-84421, r=tmandry
Dylan-DPC Apr 23, 2021
72d5ed0
Rollup merge of #84464 - jyn514:type-kind, r=CraftSpider
Dylan-DPC Apr 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
New shorter diagnostic note that is different for items versus fields
  • Loading branch information
sunjay committed Mar 25, 2021
commit 2acd8eb21f9ab5a28f7c64efcf0245691e619da0
54 changes: 41 additions & 13 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,13 @@ fn find_live<'tcx>(
symbol_visitor.live_symbols
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ExtraNote {
/// Use this to provide some examples in the diagnostic of potential other purposes for a value
/// or field that is dead code
OtherPurposeExamples,
}

struct DeadVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
live_symbols: FxHashSet<hir::HirId>,
Expand Down Expand Up @@ -573,6 +580,7 @@ impl DeadVisitor<'tcx> {
span: rustc_span::Span,
name: Symbol,
participle: &str,
extra_note: Option<ExtraNote>,
) {
if !name.as_str().starts_with('_') {
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
Expand All @@ -583,19 +591,26 @@ impl DeadVisitor<'tcx> {

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 signals to the reader that while the {} may not be {}\n\
by any Rust code, it still serves some other purpose that isn't detected by rustc.\n\
(e.g. some values are used for their effect when dropped or used in FFI code\n\
exclusively through raw pointers)",
descr, participle,
));
);

let mut note = format!(
"the leading underscore signals that this {} serves some other \
purpose\neven if it isn't used in a way that we can detect.",
descr,
);
if matches!(extra_note, Some(ExtraNote::OtherPurposeExamples)) {
note += " (e.g. for its effect\nwhen dropped or in foreign code)";
}

diag.note(&note);

// Force the note we added to the front, before any other subdiagnostics
// added in lint.build(...)
diag.children.rotate_right(1);

diag.emit()
Expand Down Expand Up @@ -644,7 +659,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
_ => "used",
};
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle);
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle, None);
} else {
// Only continue if we didn't warn
intravisit::walk_item(self, item);
Expand All @@ -658,22 +673,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
id: hir::HirId,
) {
if self.should_warn_about_variant(&variant) {
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed", None);
} else {
intravisit::walk_variant(self, variant, g, id);
}
}

fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
if self.should_warn_about_foreign_item(fi) {
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used");
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used", None);
}
intravisit::walk_foreign_item(self, fi);
}

fn visit_field_def(&mut self, field: &'tcx hir::FieldDef<'tcx>) {
if self.should_warn_about_field(&field) {
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
self.warn_dead_code(
field.hir_id,
field.span,
field.ident.name,
"read",
Some(ExtraNote::OtherPurposeExamples),
);
}
intravisit::walk_field_def(self, field);
}
Expand All @@ -687,6 +708,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
impl_item.span,
impl_item.ident.name,
"used",
None,
);
}
self.visit_nested_body(body_id)
Expand All @@ -704,7 +726,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
} else {
impl_item.ident.span
};
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident.name, "used");
self.warn_dead_code(
impl_item.hir_id(),
span,
impl_item.ident.name,
"used",
None,
);
}
self.visit_nested_body(body_id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ error: associated constant is never used: `BAR`
LL | const BAR: u32 = 1;
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_BAR`
|
= note: The leading underscore signals to the reader that while the associated constant may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this associated constant serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/associated-const-dead-code.rs:1:9
|
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/derive-uninhabited-enum-38885.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ warning: variant is never constructed: `Void`
LL | Void(Void),
| ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Void`
|
= note: The leading underscore signals to the reader that while the variant may not be constructed
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this variant serves some other purpose
even if it isn't used in a way that we can detect.
= note: `-W dead-code` implied by `-W unused`

warning: 1 warning emitted
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/issues/issue-37515.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ warning: type alias is never used: `Z`
LL | type Z = dyn for<'x> Send;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Z`
|
= note: The leading underscore signals to the reader that while the type alias may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this type alias serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/issue-37515.rs:3:9
|
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/lint/dead-code/basic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ error: function is never used: `foo`
LL | fn foo() {
| ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
|
= note: The leading underscore signals to the reader that while the function may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this function serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/basic.rs:1:9
|
Expand Down
12 changes: 4 additions & 8 deletions src/test/ui/lint/dead-code/const-and-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ warning: variant is never constructed: `B`
LL | B,
| ^ help: if this is intentional, prefix it with an underscore: `_B`
|
= note: The leading underscore signals to the reader that while the variant may not be constructed
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this variant serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/const-and-self.rs:3:9
|
Expand All @@ -20,10 +18,8 @@ warning: variant is never constructed: `C`
LL | C,
| ^ help: if this is intentional, prefix it with an underscore: `_C`
|
= note: The leading underscore signals to the reader that while the variant may not be constructed
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this variant serves some other purpose
even if it isn't used in a way that we can detect.

warning: 2 warnings emitted

7 changes: 3 additions & 4 deletions src/test/ui/lint/dead-code/drop-only-field-issue-81658.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ error: field is never read: `guard`
LL | guard: MutexGuard<'a, T>,
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_guard`
|
= note: The leading underscore signals to the reader that while the field may not be read
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this field serves some other purpose
even if it isn't used in a way that we can detect. (e.g. for its effect
when dropped or in foreign code)
note: the lint level is defined here
--> $DIR/drop-only-field-issue-81658.rs:8:9
|
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/lint/dead-code/empty-unused-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ error: enum is never used: `E`
LL | enum E {}
| ^ help: if this is intentional, prefix it with an underscore: `_E`
|
= note: The leading underscore signals to the reader that while the enum may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this enum serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/empty-unused-enum.rs:1:9
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ error: field is never read: `items`
LL | items: Option<Vec<T>>,
| ^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_items`
|
= note: The leading underscore signals to the reader that while the field may not be read
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this field serves some other purpose
even if it isn't used in a way that we can detect. (e.g. for its effect
when dropped or in foreign code)
note: the lint level is defined here
--> $DIR/field-used-in-ffi-issue-81658.rs:7:9
|
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/lint/dead-code/impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ error: type alias is never used: `Unused`
LL | type Unused = ();
| ^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Unused`
|
= note: The leading underscore signals to the reader that while the type alias may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this type alias serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/impl-trait.rs:1:9
|
Expand Down
60 changes: 20 additions & 40 deletions src/test/ui/lint/dead-code/lint-dead-code-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ error: struct is never constructed: `Bar`
LL | pub struct Bar;
| ^^^ help: if this is intentional, prefix it with an underscore: `_Bar`
|
= note: The leading underscore signals to the reader that while the struct may not be constructed
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this struct serves some other purpose
even if it isn't used in a way that we can detect.
note: the lint level is defined here
--> $DIR/lint-dead-code-1.rs:5:9
|
Expand All @@ -20,98 +18,80 @@ error: static is never used: `priv_static`
LL | static priv_static: isize = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_static`
|
= note: The leading underscore signals to the reader that while the static may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this static serves some other purpose
even if it isn't used in a way that we can detect.

error: constant is never used: `priv_const`
--> $DIR/lint-dead-code-1.rs:27:1
|
LL | const priv_const: isize = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_const`
|
= note: The leading underscore signals to the reader that while the constant may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this constant serves some other purpose
even if it isn't used in a way that we can detect.

error: struct is never constructed: `PrivStruct`
--> $DIR/lint-dead-code-1.rs:35:8
|
LL | struct PrivStruct;
| ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_PrivStruct`
|
= note: The leading underscore signals to the reader that while the struct may not be constructed
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this struct serves some other purpose
even if it isn't used in a way that we can detect.

error: enum is never used: `priv_enum`
--> $DIR/lint-dead-code-1.rs:64:6
|
LL | enum priv_enum { foo2, bar2 }
| ^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_enum`
|
= note: The leading underscore signals to the reader that while the enum may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this enum serves some other purpose
even if it isn't used in a way that we can detect.

error: variant is never constructed: `bar3`
--> $DIR/lint-dead-code-1.rs:67:5
|
LL | bar3
| ^^^^ help: if this is intentional, prefix it with an underscore: `_bar3`
|
= note: The leading underscore signals to the reader that while the variant may not be constructed
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this variant serves some other purpose
even if it isn't used in a way that we can detect.

error: function is never used: `priv_fn`
--> $DIR/lint-dead-code-1.rs:88:4
|
LL | fn priv_fn() {
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_fn`
|
= note: The leading underscore signals to the reader that while the function may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this function serves some other purpose
even if it isn't used in a way that we can detect.

error: function is never used: `foo`
--> $DIR/lint-dead-code-1.rs:93:4
|
LL | fn foo() {
| ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
|
= note: The leading underscore signals to the reader that while the function may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this function serves some other purpose
even if it isn't used in a way that we can detect.

error: function is never used: `bar`
--> $DIR/lint-dead-code-1.rs:98:4
|
LL | fn bar() {
| ^^^ help: if this is intentional, prefix it with an underscore: `_bar`
|
= note: The leading underscore signals to the reader that while the function may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this function serves some other purpose
even if it isn't used in a way that we can detect.

error: function is never used: `baz`
--> $DIR/lint-dead-code-1.rs:102:4
|
LL | fn baz() -> impl Copy {
| ^^^ help: if this is intentional, prefix it with an underscore: `_baz`
|
= note: The leading underscore signals to the reader that while the function may not be used
by any Rust code, it still serves some other purpose that isn't detected by rustc.
(e.g. some values are used for their effect when dropped or used in FFI code
exclusively through raw pointers)
= note: the leading underscore signals that this function serves some other purpose
even if it isn't used in a way that we can detect.

error: aborting due to 10 previous errors

Loading