Skip to content

Commit 74ddaf0

Browse files
committed
Avoid emitting redundant "unused label" lint
1 parent c065234 commit 74ddaf0

File tree

5 files changed

+20
-64
lines changed

5 files changed

+20
-64
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
545545
if let Some(err_code) = &err.code {
546546
if err_code == &rustc_errors::error_code!(E0425) {
547547
for label_rib in &self.label_ribs {
548-
for (label_ident, _) in &label_rib.bindings {
548+
for (label_ident, node_id) in &label_rib.bindings {
549549
if format!("'{}", ident) == label_ident.to_string() {
550550
err.span_label(label_ident.span, "a label with a similar name exists");
551551
if let PathSource::Expr(Some(Expr {
@@ -559,6 +559,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
559559
label_ident.name.to_string(),
560560
Applicability::MaybeIncorrect,
561561
);
562+
// Do not lint against unused label when we suggest them.
563+
self.diagnostic_metadata.unused_labels.remove(node_id);
562564
}
563565
}
564566
}

src/test/ui/label/label_misspelled.rs

-4
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,18 @@ fn main() {
2525

2626
fn foo() {
2727
'LOOP: loop {
28-
//~^ WARN unused label
2928
break LOOP;
3029
//~^ ERROR cannot find value `LOOP` in this scope
3130
};
3231
'while_loop: while true { //~ WARN denote infinite loops with
33-
//~^ WARN unused label
3432
break while_loop;
3533
//~^ ERROR cannot find value `while_loop` in this scope
3634
};
3735
'while_let: while let Some(_) = Some(()) {
38-
//~^ WARN unused label
3936
break while_let;
4037
//~^ ERROR cannot find value `while_let` in this scope
4138
}
4239
'for_loop: for _ in 0..3 {
43-
//~^ WARN unused label
4440
break for_loop;
4541
//~^ ERROR cannot find value `for_loop` in this scope
4642
};

src/test/ui/label/label_misspelled.stderr

+13-41
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,43 @@ LL | LOOP;
3535
| ^^^^ not found in this scope
3636

3737
error[E0425]: cannot find value `LOOP` in this scope
38-
--> $DIR/label_misspelled.rs:29:15
38+
--> $DIR/label_misspelled.rs:28:15
3939
|
4040
LL | 'LOOP: loop {
4141
| ----- a label with a similar name exists
42-
LL |
4342
LL | break LOOP;
4443
| ^^^^
4544
| |
4645
| not found in this scope
4746
| help: use the similarly named label: `'LOOP`
4847

4948
error[E0425]: cannot find value `while_loop` in this scope
50-
--> $DIR/label_misspelled.rs:34:15
49+
--> $DIR/label_misspelled.rs:32:15
5150
|
5251
LL | 'while_loop: while true {
5352
| ----------- a label with a similar name exists
54-
LL |
5553
LL | break while_loop;
5654
| ^^^^^^^^^^
5755
| |
5856
| not found in this scope
5957
| help: use the similarly named label: `'while_loop`
6058

6159
error[E0425]: cannot find value `while_let` in this scope
62-
--> $DIR/label_misspelled.rs:39:15
60+
--> $DIR/label_misspelled.rs:36:15
6361
|
6462
LL | 'while_let: while let Some(_) = Some(()) {
6563
| ---------- a label with a similar name exists
66-
LL |
6764
LL | break while_let;
6865
| ^^^^^^^^^
6966
| |
7067
| not found in this scope
7168
| help: use the similarly named label: `'while_let`
7269

7370
error[E0425]: cannot find value `for_loop` in this scope
74-
--> $DIR/label_misspelled.rs:44:15
71+
--> $DIR/label_misspelled.rs:40:15
7572
|
7673
LL | 'for_loop: for _ in 0..3 {
7774
| --------- a label with a similar name exists
78-
LL |
7975
LL | break for_loop;
8076
| ^^^^^^^^
8177
| |
@@ -120,62 +116,38 @@ warning: unused label
120116
LL | 'LOOP: loop {
121117
| ^^^^^
122118

123-
warning: unused label
124-
--> $DIR/label_misspelled.rs:27:5
125-
|
126-
LL | 'LOOP: loop {
127-
| ^^^^^
128-
129-
warning: unused label
130-
--> $DIR/label_misspelled.rs:32:5
131-
|
132-
LL | 'while_loop: while true {
133-
| ^^^^^^^^^^^
134-
135119
warning: denote infinite loops with `loop { ... }`
136-
--> $DIR/label_misspelled.rs:32:5
120+
--> $DIR/label_misspelled.rs:31:5
137121
|
138122
LL | 'while_loop: while true {
139123
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
140124

141125
warning: unused label
142-
--> $DIR/label_misspelled.rs:37:5
143-
|
144-
LL | 'while_let: while let Some(_) = Some(()) {
145-
| ^^^^^^^^^^
146-
147-
warning: unused label
148-
--> $DIR/label_misspelled.rs:42:5
149-
|
150-
LL | 'for_loop: for _ in 0..3 {
151-
| ^^^^^^^^^
152-
153-
warning: unused label
154-
--> $DIR/label_misspelled.rs:51:5
126+
--> $DIR/label_misspelled.rs:47:5
155127
|
156128
LL | 'while_loop: while true {
157129
| ^^^^^^^^^^^
158130

159131
warning: denote infinite loops with `loop { ... }`
160-
--> $DIR/label_misspelled.rs:51:5
132+
--> $DIR/label_misspelled.rs:47:5
161133
|
162134
LL | 'while_loop: while true {
163135
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
164136

165137
warning: unused label
166-
--> $DIR/label_misspelled.rs:56:5
138+
--> $DIR/label_misspelled.rs:52:5
167139
|
168140
LL | 'while_let: while let Some(_) = Some(()) {
169141
| ^^^^^^^^^^
170142

171143
warning: unused label
172-
--> $DIR/label_misspelled.rs:61:5
144+
--> $DIR/label_misspelled.rs:57:5
173145
|
174146
LL | 'for_loop: for _ in 0..3 {
175147
| ^^^^^^^^^
176148

177149
error[E0571]: `break` with value from a `while` loop
178-
--> $DIR/label_misspelled.rs:53:9
150+
--> $DIR/label_misspelled.rs:49:9
179151
|
180152
LL | 'while_loop: while true {
181153
| ----------------------- you can't `break` with a value in a `while` loop
@@ -193,7 +165,7 @@ LL | break 'while_loop;
193165
| ^^^^^^^^^^^
194166

195167
error[E0571]: `break` with value from a `while` loop
196-
--> $DIR/label_misspelled.rs:58:9
168+
--> $DIR/label_misspelled.rs:54:9
197169
|
198170
LL | 'while_let: while let Some(_) = Some(()) {
199171
| ---------------------------------------- you can't `break` with a value in a `while` loop
@@ -211,7 +183,7 @@ LL | break 'while_let;
211183
| ^^^^^^^^^^
212184

213185
error[E0571]: `break` with value from a `for` loop
214-
--> $DIR/label_misspelled.rs:63:9
186+
--> $DIR/label_misspelled.rs:59:9
215187
|
216188
LL | 'for_loop: for _ in 0..3 {
217189
| ------------------------ you can't `break` with a value in a `for` loop
@@ -228,7 +200,7 @@ help: alternatively, you might have meant to use the available loop label
228200
LL | break 'for_loop;
229201
| ^^^^^^^^^
230202

231-
error: aborting due to 11 previous errors; 14 warnings emitted
203+
error: aborting due to 11 previous errors; 10 warnings emitted
232204

233205
Some errors have detailed explanations: E0425, E0571.
234206
For more information about an error, try `rustc --explain E0425`.

src/test/ui/label/label_misspelled_2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn main() {
55
break 'a;
66
}
77
'b: for _ in 0..1 {
8-
//~^ WARN unused label
98
break b; //~ ERROR cannot find value `b` in this scope
109
}
1110
c: for _ in 0..1 { //~ ERROR expected identifier, found keyword `for`
+4-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: expected identifier, found keyword `for`
2-
--> $DIR/label_misspelled_2.rs:11:8
2+
--> $DIR/label_misspelled_2.rs:10:8
33
|
44
LL | c: for _ in 0..1 {
55
| ^^^ expected identifier, found keyword
66

77
error: expected `<`, found reserved identifier `_`
8-
--> $DIR/label_misspelled_2.rs:11:12
8+
--> $DIR/label_misspelled_2.rs:10:12
99
|
1010
LL | c: for _ in 0..1 {
1111
| - ^ expected `<`
@@ -16,29 +16,16 @@ LL | c: for _ in 0..1 {
1616
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1717

1818
error[E0425]: cannot find value `b` in this scope
19-
--> $DIR/label_misspelled_2.rs:9:15
19+
--> $DIR/label_misspelled_2.rs:8:15
2020
|
2121
LL | 'b: for _ in 0..1 {
2222
| -- a label with a similar name exists
23-
LL |
2423
LL | break b;
2524
| ^
2625
| |
2726
| not found in this scope
2827
| help: use the similarly named label: `'b`
2928

30-
warning: unused label
31-
--> $DIR/label_misspelled_2.rs:7:5
32-
|
33-
LL | 'b: for _ in 0..1 {
34-
| ^^
35-
|
36-
note: the lint level is defined here
37-
--> $DIR/label_misspelled_2.rs:1:9
38-
|
39-
LL | #![warn(unused_labels)]
40-
| ^^^^^^^^^^^^^
41-
42-
error: aborting due to 3 previous errors; 1 warning emitted
29+
error: aborting due to 3 previous errors
4330

4431
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)