Avoid emitting redundant "unused label" lint

This commit is contained in:
Esteban Küber 2021-01-20 17:57:47 -08:00
parent c065234b34
commit 74ddaf000c
5 changed files with 20 additions and 64 deletions

View File

@ -545,7 +545,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
if let Some(err_code) = &err.code { if let Some(err_code) = &err.code {
if err_code == &rustc_errors::error_code!(E0425) { if err_code == &rustc_errors::error_code!(E0425) {
for label_rib in &self.label_ribs { for label_rib in &self.label_ribs {
for (label_ident, _) in &label_rib.bindings { for (label_ident, node_id) in &label_rib.bindings {
if format!("'{}", ident) == label_ident.to_string() { if format!("'{}", ident) == label_ident.to_string() {
err.span_label(label_ident.span, "a label with a similar name exists"); err.span_label(label_ident.span, "a label with a similar name exists");
if let PathSource::Expr(Some(Expr { if let PathSource::Expr(Some(Expr {
@ -559,6 +559,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
label_ident.name.to_string(), label_ident.name.to_string(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
// Do not lint against unused label when we suggest them.
self.diagnostic_metadata.unused_labels.remove(node_id);
} }
} }
} }

View File

@ -25,22 +25,18 @@ fn main() {
fn foo() { fn foo() {
'LOOP: loop { 'LOOP: loop {
//~^ WARN unused label
break LOOP; break LOOP;
//~^ ERROR cannot find value `LOOP` in this scope //~^ ERROR cannot find value `LOOP` in this scope
}; };
'while_loop: while true { //~ WARN denote infinite loops with 'while_loop: while true { //~ WARN denote infinite loops with
//~^ WARN unused label
break while_loop; break while_loop;
//~^ ERROR cannot find value `while_loop` in this scope //~^ ERROR cannot find value `while_loop` in this scope
}; };
'while_let: while let Some(_) = Some(()) { 'while_let: while let Some(_) = Some(()) {
//~^ WARN unused label
break while_let; break while_let;
//~^ ERROR cannot find value `while_let` in this scope //~^ ERROR cannot find value `while_let` in this scope
} }
'for_loop: for _ in 0..3 { 'for_loop: for _ in 0..3 {
//~^ WARN unused label
break for_loop; break for_loop;
//~^ ERROR cannot find value `for_loop` in this scope //~^ ERROR cannot find value `for_loop` in this scope
}; };

View File

@ -35,11 +35,10 @@ LL | LOOP;
| ^^^^ not found in this scope | ^^^^ not found in this scope
error[E0425]: cannot find value `LOOP` in this scope error[E0425]: cannot find value `LOOP` in this scope
--> $DIR/label_misspelled.rs:29:15 --> $DIR/label_misspelled.rs:28:15
| |
LL | 'LOOP: loop { LL | 'LOOP: loop {
| ----- a label with a similar name exists | ----- a label with a similar name exists
LL |
LL | break LOOP; LL | break LOOP;
| ^^^^ | ^^^^
| | | |
@ -47,11 +46,10 @@ LL | break LOOP;
| help: use the similarly named label: `'LOOP` | help: use the similarly named label: `'LOOP`
error[E0425]: cannot find value `while_loop` in this scope error[E0425]: cannot find value `while_loop` in this scope
--> $DIR/label_misspelled.rs:34:15 --> $DIR/label_misspelled.rs:32:15
| |
LL | 'while_loop: while true { LL | 'while_loop: while true {
| ----------- a label with a similar name exists | ----------- a label with a similar name exists
LL |
LL | break while_loop; LL | break while_loop;
| ^^^^^^^^^^ | ^^^^^^^^^^
| | | |
@ -59,11 +57,10 @@ LL | break while_loop;
| help: use the similarly named label: `'while_loop` | help: use the similarly named label: `'while_loop`
error[E0425]: cannot find value `while_let` in this scope error[E0425]: cannot find value `while_let` in this scope
--> $DIR/label_misspelled.rs:39:15 --> $DIR/label_misspelled.rs:36:15
| |
LL | 'while_let: while let Some(_) = Some(()) { LL | 'while_let: while let Some(_) = Some(()) {
| ---------- a label with a similar name exists | ---------- a label with a similar name exists
LL |
LL | break while_let; LL | break while_let;
| ^^^^^^^^^ | ^^^^^^^^^
| | | |
@ -71,11 +68,10 @@ LL | break while_let;
| help: use the similarly named label: `'while_let` | help: use the similarly named label: `'while_let`
error[E0425]: cannot find value `for_loop` in this scope error[E0425]: cannot find value `for_loop` in this scope
--> $DIR/label_misspelled.rs:44:15 --> $DIR/label_misspelled.rs:40:15
| |
LL | 'for_loop: for _ in 0..3 { LL | 'for_loop: for _ in 0..3 {
| --------- a label with a similar name exists | --------- a label with a similar name exists
LL |
LL | break for_loop; LL | break for_loop;
| ^^^^^^^^ | ^^^^^^^^
| | | |
@ -120,62 +116,38 @@ warning: unused label
LL | 'LOOP: loop { LL | 'LOOP: loop {
| ^^^^^ | ^^^^^
warning: unused label
--> $DIR/label_misspelled.rs:27:5
|
LL | 'LOOP: loop {
| ^^^^^
warning: unused label
--> $DIR/label_misspelled.rs:32:5
|
LL | 'while_loop: while true {
| ^^^^^^^^^^^
warning: denote infinite loops with `loop { ... }` warning: denote infinite loops with `loop { ... }`
--> $DIR/label_misspelled.rs:32:5 --> $DIR/label_misspelled.rs:31:5
| |
LL | 'while_loop: while true { LL | 'while_loop: while true {
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop` | ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
warning: unused label warning: unused label
--> $DIR/label_misspelled.rs:37:5 --> $DIR/label_misspelled.rs:47:5
|
LL | 'while_let: while let Some(_) = Some(()) {
| ^^^^^^^^^^
warning: unused label
--> $DIR/label_misspelled.rs:42:5
|
LL | 'for_loop: for _ in 0..3 {
| ^^^^^^^^^
warning: unused label
--> $DIR/label_misspelled.rs:51:5
| |
LL | 'while_loop: while true { LL | 'while_loop: while true {
| ^^^^^^^^^^^ | ^^^^^^^^^^^
warning: denote infinite loops with `loop { ... }` warning: denote infinite loops with `loop { ... }`
--> $DIR/label_misspelled.rs:51:5 --> $DIR/label_misspelled.rs:47:5
| |
LL | 'while_loop: while true { LL | 'while_loop: while true {
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop` | ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
warning: unused label warning: unused label
--> $DIR/label_misspelled.rs:56:5 --> $DIR/label_misspelled.rs:52:5
| |
LL | 'while_let: while let Some(_) = Some(()) { LL | 'while_let: while let Some(_) = Some(()) {
| ^^^^^^^^^^ | ^^^^^^^^^^
warning: unused label warning: unused label
--> $DIR/label_misspelled.rs:61:5 --> $DIR/label_misspelled.rs:57:5
| |
LL | 'for_loop: for _ in 0..3 { LL | 'for_loop: for _ in 0..3 {
| ^^^^^^^^^ | ^^^^^^^^^
error[E0571]: `break` with value from a `while` loop error[E0571]: `break` with value from a `while` loop
--> $DIR/label_misspelled.rs:53:9 --> $DIR/label_misspelled.rs:49:9
| |
LL | 'while_loop: while true { LL | 'while_loop: while true {
| ----------------------- you can't `break` with a value in a `while` loop | ----------------------- you can't `break` with a value in a `while` loop
@ -193,7 +165,7 @@ LL | break 'while_loop;
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error[E0571]: `break` with value from a `while` loop error[E0571]: `break` with value from a `while` loop
--> $DIR/label_misspelled.rs:58:9 --> $DIR/label_misspelled.rs:54:9
| |
LL | 'while_let: while let Some(_) = Some(()) { LL | 'while_let: while let Some(_) = Some(()) {
| ---------------------------------------- you can't `break` with a value in a `while` loop | ---------------------------------------- you can't `break` with a value in a `while` loop
@ -211,7 +183,7 @@ LL | break 'while_let;
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0571]: `break` with value from a `for` loop error[E0571]: `break` with value from a `for` loop
--> $DIR/label_misspelled.rs:63:9 --> $DIR/label_misspelled.rs:59:9
| |
LL | 'for_loop: for _ in 0..3 { LL | 'for_loop: for _ in 0..3 {
| ------------------------ you can't `break` with a value in a `for` loop | ------------------------ 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
LL | break 'for_loop; LL | break 'for_loop;
| ^^^^^^^^^ | ^^^^^^^^^
error: aborting due to 11 previous errors; 14 warnings emitted error: aborting due to 11 previous errors; 10 warnings emitted
Some errors have detailed explanations: E0425, E0571. Some errors have detailed explanations: E0425, E0571.
For more information about an error, try `rustc --explain E0425`. For more information about an error, try `rustc --explain E0425`.

View File

@ -5,7 +5,6 @@ fn main() {
break 'a; break 'a;
} }
'b: for _ in 0..1 { 'b: for _ in 0..1 {
//~^ WARN unused label
break b; //~ ERROR cannot find value `b` in this scope break b; //~ ERROR cannot find value `b` in this scope
} }
c: for _ in 0..1 { //~ ERROR expected identifier, found keyword `for` c: for _ in 0..1 { //~ ERROR expected identifier, found keyword `for`

View File

@ -1,11 +1,11 @@
error: expected identifier, found keyword `for` error: expected identifier, found keyword `for`
--> $DIR/label_misspelled_2.rs:11:8 --> $DIR/label_misspelled_2.rs:10:8
| |
LL | c: for _ in 0..1 { LL | c: for _ in 0..1 {
| ^^^ expected identifier, found keyword | ^^^ expected identifier, found keyword
error: expected `<`, found reserved identifier `_` error: expected `<`, found reserved identifier `_`
--> $DIR/label_misspelled_2.rs:11:12 --> $DIR/label_misspelled_2.rs:10:12
| |
LL | c: for _ in 0..1 { LL | c: for _ in 0..1 {
| - ^ expected `<` | - ^ expected `<`
@ -16,29 +16,16 @@ LL | c: for _ in 0..1 {
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information = note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
error[E0425]: cannot find value `b` in this scope error[E0425]: cannot find value `b` in this scope
--> $DIR/label_misspelled_2.rs:9:15 --> $DIR/label_misspelled_2.rs:8:15
| |
LL | 'b: for _ in 0..1 { LL | 'b: for _ in 0..1 {
| -- a label with a similar name exists | -- a label with a similar name exists
LL |
LL | break b; LL | break b;
| ^ | ^
| | | |
| not found in this scope | not found in this scope
| help: use the similarly named label: `'b` | help: use the similarly named label: `'b`
warning: unused label error: aborting due to 3 previous errors
--> $DIR/label_misspelled_2.rs:7:5
|
LL | 'b: for _ in 0..1 {
| ^^
|
note: the lint level is defined here
--> $DIR/label_misspelled_2.rs:1:9
|
LL | #![warn(unused_labels)]
| ^^^^^^^^^^^^^
error: aborting due to 3 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0425`. For more information about this error, try `rustc --explain E0425`.