Skip to content

Commit 2acd8eb

Browse files
committed
New shorter diagnostic note that is different for items versus fields
1 parent 789186d commit 2acd8eb

29 files changed

+207
-309
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ fn find_live<'tcx>(
506506
symbol_visitor.live_symbols
507507
}
508508

509+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
510+
enum ExtraNote {
511+
/// Use this to provide some examples in the diagnostic of potential other purposes for a value
512+
/// or field that is dead code
513+
OtherPurposeExamples,
514+
}
515+
509516
struct DeadVisitor<'tcx> {
510517
tcx: TyCtxt<'tcx>,
511518
live_symbols: FxHashSet<hir::HirId>,
@@ -573,6 +580,7 @@ impl DeadVisitor<'tcx> {
573580
span: rustc_span::Span,
574581
name: Symbol,
575582
participle: &str,
583+
extra_note: Option<ExtraNote>,
576584
) {
577585
if !name.as_str().starts_with('_') {
578586
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
@@ -583,19 +591,26 @@ impl DeadVisitor<'tcx> {
583591

584592
let mut diag =
585593
lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
594+
586595
diag.multipart_suggestion(
587596
"if this is intentional, prefix it with an underscore",
588597
prefixed,
589598
Applicability::MachineApplicable,
590-
)
591-
.note(&format!(
592-
"The leading underscore signals to the reader that while the {} may not be {}\n\
593-
by any Rust code, it still serves some other purpose that isn't detected by rustc.\n\
594-
(e.g. some values are used for their effect when dropped or used in FFI code\n\
595-
exclusively through raw pointers)",
596-
descr, participle,
597-
));
599+
);
600+
601+
let mut note = format!(
602+
"the leading underscore signals that this {} serves some other \
603+
purpose\neven if it isn't used in a way that we can detect.",
604+
descr,
605+
);
606+
if matches!(extra_note, Some(ExtraNote::OtherPurposeExamples)) {
607+
note += " (e.g. for its effect\nwhen dropped or in foreign code)";
608+
}
609+
610+
diag.note(&note);
611+
598612
// Force the note we added to the front, before any other subdiagnostics
613+
// added in lint.build(...)
599614
diag.children.rotate_right(1);
600615

601616
diag.emit()
@@ -644,7 +659,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
644659
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
645660
_ => "used",
646661
};
647-
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle);
662+
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle, None);
648663
} else {
649664
// Only continue if we didn't warn
650665
intravisit::walk_item(self, item);
@@ -658,22 +673,28 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
658673
id: hir::HirId,
659674
) {
660675
if self.should_warn_about_variant(&variant) {
661-
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
676+
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed", None);
662677
} else {
663678
intravisit::walk_variant(self, variant, g, id);
664679
}
665680
}
666681

667682
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
668683
if self.should_warn_about_foreign_item(fi) {
669-
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used");
684+
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used", None);
670685
}
671686
intravisit::walk_foreign_item(self, fi);
672687
}
673688

674689
fn visit_field_def(&mut self, field: &'tcx hir::FieldDef<'tcx>) {
675690
if self.should_warn_about_field(&field) {
676-
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
691+
self.warn_dead_code(
692+
field.hir_id,
693+
field.span,
694+
field.ident.name,
695+
"read",
696+
Some(ExtraNote::OtherPurposeExamples),
697+
);
677698
}
678699
intravisit::walk_field_def(self, field);
679700
}
@@ -687,6 +708,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
687708
impl_item.span,
688709
impl_item.ident.name,
689710
"used",
711+
None,
690712
);
691713
}
692714
self.visit_nested_body(body_id)
@@ -704,7 +726,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
704726
} else {
705727
impl_item.ident.span
706728
};
707-
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident.name, "used");
729+
self.warn_dead_code(
730+
impl_item.hir_id(),
731+
span,
732+
impl_item.ident.name,
733+
"used",
734+
None,
735+
);
708736
}
709737
self.visit_nested_body(body_id)
710738
}

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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 4 additions & 8 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 2 additions & 4 deletions
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
|

0 commit comments

Comments
 (0)