Skip to content

Commit 0fb9c23

Browse files
committed
New shorter diagnostic note that is different for items versus fields
1 parent f8e6911 commit 0fb9c23

29 files changed

+207
-309
lines changed

compiler/rustc_passes/src/dead.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ fn find_live<'tcx>(
517517
symbol_visitor.live_symbols
518518
}
519519

520+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
521+
enum ExtraNote {
522+
/// Use this to provide some examples in the diagnostic of potential other purposes for a value
523+
/// or field that is dead code
524+
OtherPurposeExamples,
525+
}
526+
520527
struct DeadVisitor<'tcx> {
521528
tcx: TyCtxt<'tcx>,
522529
live_symbols: FxHashSet<hir::HirId>,
@@ -584,6 +591,7 @@ impl DeadVisitor<'tcx> {
584591
span: rustc_span::Span,
585592
name: Symbol,
586593
participle: &str,
594+
extra_note: Option<ExtraNote>,
587595
) {
588596
if !name.as_str().starts_with('_') {
589597
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
@@ -594,19 +602,26 @@ impl DeadVisitor<'tcx> {
594602

595603
let mut diag =
596604
lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
605+
597606
diag.multipart_suggestion(
598607
"if this is intentional, prefix it with an underscore",
599608
prefixed,
600609
Applicability::MachineApplicable,
601-
)
602-
.note(&format!(
603-
"The leading underscore signals to the reader that while the {} may not be {}\n\
604-
by any Rust code, it still serves some other purpose that isn't detected by rustc.\n\
605-
(e.g. some values are used for their effect when dropped or used in FFI code\n\
606-
exclusively through raw pointers)",
607-
descr, participle,
608-
));
610+
);
611+
612+
let mut note = format!(
613+
"the leading underscore signals that this {} serves some other \
614+
purpose\neven if it isn't used in a way that we can detect.",
615+
descr,
616+
);
617+
if matches!(extra_note, Some(ExtraNote::OtherPurposeExamples)) {
618+
note += " (e.g. for its effect\nwhen dropped or in foreign code)";
619+
}
620+
621+
diag.note(&note);
622+
609623
// Force the note we added to the front, before any other subdiagnostics
624+
// added in lint.build(...)
610625
diag.children.rotate_right(1);
611626

612627
diag.emit()
@@ -655,7 +670,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
655670
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
656671
_ => "used",
657672
};
658-
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle);
673+
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle, None);
659674
} else {
660675
// Only continue if we didn't warn
661676
intravisit::walk_item(self, item);
@@ -669,22 +684,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
669684
id: hir::HirId,
670685
) {
671686
if self.should_warn_about_variant(&variant) {
672-
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
687+
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed", None);
673688
} else {
674689
intravisit::walk_variant(self, variant, g, id);
675690
}
676691
}
677692

678693
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
679694
if self.should_warn_about_foreign_item(fi) {
680-
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used");
695+
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used", None);
681696
}
682697
intravisit::walk_foreign_item(self, fi);
683698
}
684699

685700
fn visit_field_def(&mut self, field: &'tcx hir::FieldDef<'tcx>) {
686701
if self.should_warn_about_field(&field) {
687-
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
702+
self.warn_dead_code(
703+
field.hir_id,
704+
field.span,
705+
field.ident.name,
706+
"read",
707+
Some(ExtraNote::OtherPurposeExamples),
708+
);
688709
}
689710
intravisit::walk_field_def(self, field);
690711
}
@@ -698,6 +719,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
698719
impl_item.span,
699720
impl_item.ident.name,
700721
"used",
722+
None,
701723
);
702724
}
703725
self.visit_nested_body(body_id)
@@ -715,7 +737,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
715737
} else {
716738
impl_item.ident.span
717739
};
718-
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident.name, "used");
740+
self.warn_dead_code(
741+
impl_item.hir_id(),
742+
span,
743+
impl_item.ident.name,
744+
"used",
745+
None,
746+
);
719747
}
720748
self.visit_nested_body(body_id)
721749
}

src/test/ui/associated-consts/associated-const-dead-code.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ error: associated constant is never used: `BAR`
44
LL | const BAR: u32 = 1;
55
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_BAR`
66
|
7-
= note: The leading underscore signals to the reader that while the associated constant may not be used
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this associated constant serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/associated-const-dead-code.rs:1:9
1311
|

src/test/ui/derive-uninhabited-enum-38885.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ warning: variant is never constructed: `Void`
44
LL | Void(Void),
55
| ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Void`
66
|
7-
= note: The leading underscore signals to the reader that while the variant may not be constructed
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this variant serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
= note: `-W dead-code` implied by `-W unused`
1210

1311
warning: 1 warning emitted

src/test/ui/issues/issue-37515.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ warning: type alias is never used: `Z`
44
LL | type Z = dyn for<'x> Send;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Z`
66
|
7-
= note: The leading underscore signals to the reader that while the type alias may not be used
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this type alias serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/issue-37515.rs:3:9
1311
|

src/test/ui/lint/dead-code/basic.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ error: function is never used: `foo`
44
LL | fn foo() {
55
| ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
66
|
7-
= note: The leading underscore signals to the reader that while the function may not be used
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this function serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/basic.rs:1:9
1311
|

src/test/ui/lint/dead-code/const-and-self.stderr

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ warning: variant is never constructed: `B`
44
LL | B,
55
| ^ help: if this is intentional, prefix it with an underscore: `_B`
66
|
7-
= note: The leading underscore signals to the reader that while the variant may not be constructed
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this variant serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/const-and-self.rs:3:9
1311
|
@@ -20,10 +18,8 @@ warning: variant is never constructed: `C`
2018
LL | C,
2119
| ^ help: if this is intentional, prefix it with an underscore: `_C`
2220
|
23-
= note: The leading underscore signals to the reader that while the variant may not be constructed
24-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
25-
(e.g. some values are used for their effect when dropped or used in FFI code
26-
exclusively through raw pointers)
21+
= note: the leading underscore signals that this variant serves some other purpose
22+
even if it isn't used in a way that we can detect.
2723

2824
warning: 2 warnings emitted
2925

src/test/ui/lint/dead-code/drop-only-field-issue-81658.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ error: field is never read: `guard`
44
LL | guard: MutexGuard<'a, T>,
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_guard`
66
|
7-
= note: The leading underscore signals to the reader that while the field may not be read
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this field serves some other purpose
8+
even if it isn't used in a way that we can detect. (e.g. for its effect
9+
when dropped or in foreign code)
1110
note: the lint level is defined here
1211
--> $DIR/drop-only-field-issue-81658.rs:8:9
1312
|

src/test/ui/lint/dead-code/empty-unused-enum.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ error: enum is never used: `E`
44
LL | enum E {}
55
| ^ help: if this is intentional, prefix it with an underscore: `_E`
66
|
7-
= note: The leading underscore signals to the reader that while the enum may not be used
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this enum serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/empty-unused-enum.rs:1:9
1311
|

src/test/ui/lint/dead-code/field-used-in-ffi-issue-81658.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ error: field is never read: `items`
44
LL | items: Option<Vec<T>>,
55
| ^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_items`
66
|
7-
= note: The leading underscore signals to the reader that while the field may not be read
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this field serves some other purpose
8+
even if it isn't used in a way that we can detect. (e.g. for its effect
9+
when dropped or in foreign code)
1110
note: the lint level is defined here
1211
--> $DIR/field-used-in-ffi-issue-81658.rs:7:9
1312
|

src/test/ui/lint/dead-code/impl-trait.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ error: type alias is never used: `Unused`
44
LL | type Unused = ();
55
| ^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Unused`
66
|
7-
= note: The leading underscore signals to the reader that while the type alias may not be used
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this type alias serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/impl-trait.rs:1:9
1311
|

src/test/ui/lint/dead-code/lint-dead-code-1.stderr

+20-40
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ error: struct is never constructed: `Bar`
44
LL | pub struct Bar;
55
| ^^^ help: if this is intentional, prefix it with an underscore: `_Bar`
66
|
7-
= note: The leading underscore signals to the reader that while the struct may not be constructed
8-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
9-
(e.g. some values are used for their effect when dropped or used in FFI code
10-
exclusively through raw pointers)
7+
= note: the leading underscore signals that this struct serves some other purpose
8+
even if it isn't used in a way that we can detect.
119
note: the lint level is defined here
1210
--> $DIR/lint-dead-code-1.rs:5:9
1311
|
@@ -20,98 +18,80 @@ error: static is never used: `priv_static`
2018
LL | static priv_static: isize = 0;
2119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_static`
2220
|
23-
= note: The leading underscore signals to the reader that while the static may not be used
24-
by any Rust code, it still serves some other purpose that isn't detected by rustc.
25-
(e.g. some values are used for their effect when dropped or used in FFI code
26-
exclusively through raw pointers)
21+
= note: the leading underscore signals that this static serves some other purpose
22+
even if it isn't used in a way that we can detect.
2723

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

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

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

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

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

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

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

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

11696
error: aborting due to 10 previous errors
11797

0 commit comments

Comments
 (0)