Skip to content

Commit 0ba2c6a

Browse files
committed
Putting help message only under the identifier that needs to be prefixed
1 parent bacfc34 commit 0ba2c6a

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
@@ -580,7 +580,7 @@ impl DeadVisitor<'tcx> {
580580
&mut self,
581581
id: hir::HirId,
582582
span: rustc_span::Span,
583-
name: Symbol,
583+
name: Ident,
584584
participle: &str,
585585
extra_note: Option<ExtraNote>,
586586
) {
@@ -589,7 +589,7 @@ impl DeadVisitor<'tcx> {
589589
let def_id = self.tcx.hir().local_def_id(id);
590590
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
591591

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

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

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

612612
diag.note(&note);
@@ -661,7 +661,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
661661
hir::ItemKind::Struct(..) => "constructed", // Issue #52325
662662
_ => "used",
663663
};
664-
self.warn_dead_code(item.hir_id(), span, item.ident.name, participle, None);
664+
self.warn_dead_code(item.hir_id(), span, item.ident, participle, None);
665665
} else {
666666
// Only continue if we didn't warn
667667
intravisit::walk_item(self, item);
@@ -675,15 +675,15 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
675675
id: hir::HirId,
676676
) {
677677
if self.should_warn_about_variant(&variant) {
678-
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed", None);
678+
self.warn_dead_code(variant.id, variant.span, variant.ident, "constructed", None);
679679
} else {
680680
intravisit::walk_variant(self, variant, g, id);
681681
}
682682
}
683683

684684
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) {
685685
if self.should_warn_about_foreign_item(fi) {
686-
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident.name, "used", None);
686+
self.warn_dead_code(fi.hir_id(), fi.span, fi.ident, "used", None);
687687
}
688688
intravisit::walk_foreign_item(self, fi);
689689
}
@@ -693,7 +693,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
693693
self.warn_dead_code(
694694
field.hir_id,
695695
field.span,
696-
field.ident.name,
696+
field.ident,
697697
"read",
698698
Some(ExtraNote::OtherPurposeExamples),
699699
);
@@ -708,7 +708,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
708708
self.warn_dead_code(
709709
impl_item.hir_id(),
710710
impl_item.span,
711-
impl_item.ident.name,
711+
impl_item.ident,
712712
"used",
713713
None,
714714
);
@@ -728,13 +728,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
728728
} else {
729729
impl_item.ident.span
730730
};
731-
self.warn_dead_code(
732-
impl_item.hir_id(),
733-
span,
734-
impl_item.ident.name,
735-
"used",
736-
None,
737-
);
731+
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident, "used", None);
738732
}
739733
self.visit_nested_body(body_id)
740734
}

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)