Skip to content

Commit 33f31eb

Browse files
committed
Putting help message only under the identifier that needs to be prefixed
1 parent 0fb9c23 commit 33f31eb

29 files changed

+183
-216
lines changed

compiler/rustc_passes/src/dead.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::middle::privacy;
1616
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
1717
use rustc_session::lint;
1818

19-
use rustc_span::symbol::{sym, Symbol};
19+
use rustc_span::symbol::{sym, Ident, Symbol};
2020

2121
// Any local node that may call something in its body block should be
2222
// explored. For example, if it's a live Node::Item that is a
@@ -589,7 +589,7 @@ impl DeadVisitor<'tcx> {
589589
&mut self,
590590
id: hir::HirId,
591591
span: rustc_span::Span,
592-
name: Symbol,
592+
name: Ident,
593593
participle: &str,
594594
extra_note: Option<ExtraNote>,
595595
) {
@@ -598,7 +598,7 @@ impl DeadVisitor<'tcx> {
598598
let def_id = self.tcx.hir().local_def_id(id);
599599
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
600600

601-
let prefixed = vec![(span, format!("_{}", name))];
601+
let prefixed = vec![(name.span, format!("_{}", name))];
602602

603603
let mut diag =
604604
lint.build(&format!("{} is never {}: `{}`", descr, participle, name));
@@ -611,11 +611,11 @@ impl DeadVisitor<'tcx> {
611611

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

621621
diag.note(&note);
@@ -670,7 +670,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
670670
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
671671
_ => "used",
672672
};
673-
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle, None);
673+
self.warn_dead_code(item.hir_id(), span, item.ident, participle, None);
674674
} else {
675675
// Only continue if we didn't warn
676676
intravisit::walk_item(self, item);
@@ -684,15 +684,15 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
684684
id: hir::HirId,
685685
) {
686686
if self.should_warn_about_variant(&variant) {
687-
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed", None);
687+
self.warn_dead_code(variant.id, variant.span, variant.ident, "constructed", None);
688688
} else {
689689
intravisit::walk_variant(self, variant, g, id);
690690
}
691691
}
692692

693693
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
694694
if self.should_warn_about_foreign_item(fi) {
695-
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used", None);
695+
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident, "used", None);
696696
}
697697
intravisit::walk_foreign_item(self, fi);
698698
}
@@ -702,7 +702,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
702702
self.warn_dead_code(
703703
field.hir_id,
704704
field.span,
705-
field.ident.name,
705+
field.ident,
706706
"read",
707707
Some(ExtraNote::OtherPurposeExamples),
708708
);
@@ -717,7 +717,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
717717
self.warn_dead_code(
718718
impl_item.hir_id(),
719719
impl_item.span,
720-
impl_item.ident.name,
720+
impl_item.ident,
721721
"used",
722722
None,
723723
);
@@ -737,13 +737,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
737737
} else {
738738
impl_item.ident.span
739739
};
740-
self.warn_dead_code(
741-
impl_item.hir_id(),
742-
span,
743-
impl_item.ident.name,
744-
"used",
745-
None,
746-
);
740+
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident, "used", None);
747741
}
748742
self.visit_nested_body(body_id)
749743
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ error: associated constant is never used: `BAR`
22
--> $DIR/associated-const-dead-code.rs:6:5
33
|
44
LL | const BAR: u32 = 1;
5-
| ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_BAR`
5+
| ^^^^^^---^^^^^^^^^^
6+
| |
7+
| help: if this is intentional, prefix it with an underscore: `_BAR`
68
|
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.
9+
= 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.
910
note: the lint level is defined here
1011
--> $DIR/associated-const-dead-code.rs:1:9
1112
|

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ warning: variant is never constructed: `Void`
22
--> $DIR/derive-uninhabited-enum-38885.rs:13:5
33
|
44
LL | Void(Void),
5-
| ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Void`
5+
| ----^^^^^^
6+
| |
7+
| help: if this is intentional, prefix it with an underscore: `_Void`
68
|
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.
9+
= 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.
910
= note: `-W dead-code` implied by `-W unused`
1011

1112
warning: 1 warning emitted

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ warning: type alias is never used: `Z`
22
--> $DIR/issue-37515.rs:5:1
33
|
44
LL | type Z = dyn for<'x> Send;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Z`
5+
| ^^^^^-^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| help: if this is intentional, prefix it with an underscore: `_Z`
68
|
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.
9+
= 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.
910
note: the lint level is defined here
1011
--> $DIR/issue-37515.rs:3:9
1112
|

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ 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 that this function serves some other purpose
8-
even if it isn't used in a way that we can detect.
7+
= 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.
98
note: the lint level is defined here
109
--> $DIR/basic.rs:1:9
1110
|

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ 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 that this variant serves some other purpose
8-
even if it isn't used in a way that we can detect.
7+
= 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.
98
note: the lint level is defined here
109
--> $DIR/const-and-self.rs:3:9
1110
|
@@ -18,8 +17,7 @@ warning: variant is never constructed: `C`
1817
LL | C,
1918
| ^ help: if this is intentional, prefix it with an underscore: `_C`
2019
|
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.
20+
= 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.
2321

2422
warning: 2 warnings emitted
2523

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ error: field is never read: `guard`
22
--> $DIR/drop-only-field-issue-81658.rs:15:5
33
|
44
LL | guard: MutexGuard<'a, T>,
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_guard`
5+
| -----^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| help: if this is intentional, prefix it with an underscore: `_guard`
68
|
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)
9+
= 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)
1010
note: the lint level is defined here
1111
--> $DIR/drop-only-field-issue-81658.rs:8:9
1212
|

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ 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 that this enum serves some other purpose
8-
even if it isn't used in a way that we can detect.
7+
= 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.
98
note: the lint level is defined here
109
--> $DIR/empty-unused-enum.rs:1:9
1110
|

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ error: field is never read: `items`
22
--> $DIR/field-used-in-ffi-issue-81658.rs:13:5
33
|
44
LL | items: Option<Vec<T>>,
5-
| ^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_items`
5+
| -----^^^^^^^^^^^^^^^^
6+
| |
7+
| help: if this is intentional, prefix it with an underscore: `_items`
68
|
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)
9+
= 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)
1010
note: the lint level is defined here
1111
--> $DIR/field-used-in-ffi-issue-81658.rs:7:9
1212
|

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ error: type alias is never used: `Unused`
22
--> $DIR/impl-trait.rs:12:1
33
|
44
LL | type Unused = ();
5-
| ^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_Unused`
5+
| ^^^^^------^^^^^^
6+
| |
7+
| help: if this is intentional, prefix it with an underscore: `_Unused`
68
|
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.
9+
= 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.
910
note: the lint level is defined here
1011
--> $DIR/impl-trait.rs:1:9
1112
|

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

+16-22
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ 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 that this struct serves some other purpose
8-
even if it isn't used in a way that we can detect.
7+
= 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.
98
note: the lint level is defined here
109
--> $DIR/lint-dead-code-1.rs:5:9
1110
|
@@ -16,82 +15,77 @@ error: static is never used: `priv_static`
1615
--> $DIR/lint-dead-code-1.rs:20:1
1716
|
1817
LL | static priv_static: isize = 0;
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_static`
18+
| ^^^^^^^-----------^^^^^^^^^^^^
19+
| |
20+
| help: if this is intentional, prefix it with an underscore: `_priv_static`
2021
|
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.
22+
= 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.
2323

2424
error: constant is never used: `priv_const`
2525
--> $DIR/lint-dead-code-1.rs:27:1
2626
|
2727
LL | const priv_const: isize = 0;
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_const`
28+
| ^^^^^^----------^^^^^^^^^^^^
29+
| |
30+
| help: if this is intentional, prefix it with an underscore: `_priv_const`
2931
|
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.
32+
= 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.
3233

3334
error: struct is never constructed: `PrivStruct`
3435
--> $DIR/lint-dead-code-1.rs:35:8
3536
|
3637
LL | struct PrivStruct;
3738
| ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_PrivStruct`
3839
|
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.
40+
= 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.
4141

4242
error: enum is never used: `priv_enum`
4343
--> $DIR/lint-dead-code-1.rs:64:6
4444
|
4545
LL | enum priv_enum { foo2, bar2 }
4646
| ^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_enum`
4747
|
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.
48+
= 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.
5049

5150
error: variant is never constructed: `bar3`
5251
--> $DIR/lint-dead-code-1.rs:67:5
5352
|
5453
LL | bar3
5554
| ^^^^ help: if this is intentional, prefix it with an underscore: `_bar3`
5655
|
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.
56+
= 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.
5957

6058
error: function is never used: `priv_fn`
6159
--> $DIR/lint-dead-code-1.rs:88:4
6260
|
6361
LL | fn priv_fn() {
6462
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_priv_fn`
6563
|
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.
64+
= 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.
6865

6966
error: function is never used: `foo`
7067
--> $DIR/lint-dead-code-1.rs:93:4
7168
|
7269
LL | fn foo() {
7370
| ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
7471
|
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.
72+
= 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.
7773

7874
error: function is never used: `bar`
7975
--> $DIR/lint-dead-code-1.rs:98:4
8076
|
8177
LL | fn bar() {
8278
| ^^^ help: if this is intentional, prefix it with an underscore: `_bar`
8379
|
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.
80+
= 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.
8681

8782
error: function is never used: `baz`
8883
--> $DIR/lint-dead-code-1.rs:102:4
8984
|
9085
LL | fn baz() -> impl Copy {
9186
| ^^^ help: if this is intentional, prefix it with an underscore: `_baz`
9287
|
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.
88+
= 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.
9589

9690
error: aborting due to 10 previous errors
9791

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ error: function is never used: `dead_fn`
44
LL | fn dead_fn() {}
55
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_dead_fn`
66
|
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.
7+
= 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.
98
note: the lint level is defined here
109
--> $DIR/lint-dead-code-2.rs:2:9
1110
|
@@ -18,17 +17,15 @@ error: function is never used: `dead_fn2`
1817
LL | fn dead_fn2() {}
1918
| ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_dead_fn2`
2019
|
21-
= note: the leading underscore signals that this function serves some other purpose
22-
even if it isn't used in a way that we can detect.
20+
= 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.
2321

2422
error: function is never used: `main`
2523
--> $DIR/lint-dead-code-2.rs:38:4
2624
|
2725
LL | fn main() {
2826
| ^^^^ help: if this is intentional, prefix it with an underscore: `_main`
2927
|
30-
= note: the leading underscore signals that this function serves some other purpose
31-
even if it isn't used in a way that we can detect.
28+
= 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.
3229

3330
error: aborting due to 3 previous errors
3431

0 commit comments

Comments
 (0)