-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #50763 - KyleStach1678:unused-loop-label, r=petrochenkov
Add lint checks for unused loop labels Previously no warning was generated when a loop label was written out but never used (i.e. in a `break` or `continue` statement): ```rust fn main() { 'unused_label: loop {} } ``` would compile without complaint. This fix introduces `-W unused_loop_label`, which generates the following warning message for the above snippet: ``` warning: unused loop label --> main.rs:2:5 | 2 | 'unused_label: loop {} | ^^^^^^^^^^^^^ | = note: #[warn(unused_loop_label)] on by default ``` Fixes: #50751.
- Loading branch information
Showing
15 changed files
with
210 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// The output should warn when a loop label is not used. However, it | ||
// should also deal with the edge cases where a label is shadowed, | ||
// within nested loops | ||
|
||
// compile-pass | ||
|
||
#![feature(label_break_value)] | ||
#![warn(unused_labels)] | ||
|
||
fn main() { | ||
'unused_while_label: while 0 == 0 { | ||
//~^ WARN unused label | ||
} | ||
|
||
let opt = Some(0); | ||
'unused_while_let_label: while let Some(_) = opt { | ||
//~^ WARN unused label | ||
} | ||
|
||
'unused_for_label: for _ in 0..10 { | ||
//~^ WARN unused label | ||
} | ||
|
||
'used_loop_label: loop { | ||
break 'used_loop_label; | ||
} | ||
|
||
'used_loop_label_outer_1: for _ in 0..10 { | ||
'used_loop_label_inner_1: for _ in 0..10 { | ||
break 'used_loop_label_inner_1; | ||
} | ||
break 'used_loop_label_outer_1; | ||
} | ||
|
||
'used_loop_label_outer_2: for _ in 0..10 { | ||
'unused_loop_label_inner_2: for _ in 0..10 { | ||
//~^ WARN unused label | ||
break 'used_loop_label_outer_2; | ||
} | ||
} | ||
|
||
'unused_loop_label_outer_3: for _ in 0..10 { | ||
//~^ WARN unused label | ||
'used_loop_label_inner_3: for _ in 0..10 { | ||
break 'used_loop_label_inner_3; | ||
} | ||
} | ||
|
||
// You should be able to break the same label many times | ||
'many_used: loop { | ||
if true { | ||
break 'many_used; | ||
} else { | ||
break 'many_used; | ||
} | ||
} | ||
|
||
// Test breaking many times with the same inner label doesn't break the | ||
// warning on the outer label | ||
'many_used_shadowed: for _ in 0..10 { | ||
//~^ WARN unused label | ||
'many_used_shadowed: for _ in 0..10 { | ||
//~^ WARN label name `'many_used_shadowed` shadows a label name that is already in scope | ||
if 1 % 2 == 0 { | ||
break 'many_used_shadowed; | ||
} else { | ||
break 'many_used_shadowed; | ||
} | ||
} | ||
} | ||
|
||
'unused_loop_label: loop { | ||
//~^ WARN unused label | ||
break; | ||
} | ||
|
||
// Make sure unused block labels give warnings... | ||
'unused_block_label: { | ||
//~^ WARN unused label | ||
} | ||
|
||
// ...and that used ones don't: | ||
'used_block_label: { | ||
break 'used_block_label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
warning: unused label | ||
--> $DIR/unused_labels.rs:21:5 | ||
| | ||
LL | 'unused_while_label: while 0 == 0 { | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/unused_labels.rs:18:9 | ||
| | ||
LL | #![warn(unused_labels)] | ||
| ^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:26:5 | ||
| | ||
LL | 'unused_while_let_label: while let Some(_) = opt { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:30:5 | ||
| | ||
LL | 'unused_for_label: for _ in 0..10 { | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:46:9 | ||
| | ||
LL | 'unused_loop_label_inner_2: for _ in 0..10 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:52:5 | ||
| | ||
LL | 'unused_loop_label_outer_3: for _ in 0..10 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:70:5 | ||
| | ||
LL | 'many_used_shadowed: for _ in 0..10 { | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:82:5 | ||
| | ||
LL | 'unused_loop_label: loop { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: unused label | ||
--> $DIR/unused_labels.rs:88:5 | ||
| | ||
LL | 'unused_block_label: { | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
warning: label name `'many_used_shadowed` shadows a label name that is already in scope | ||
--> $DIR/unused_labels.rs:72:9 | ||
| | ||
LL | 'many_used_shadowed: for _ in 0..10 { | ||
| ------------------- first declared here | ||
LL | //~^ WARN unused label | ||
LL | 'many_used_shadowed: for _ in 0..10 { | ||
| ^^^^^^^^^^^^^^^^^^^ lifetime 'many_used_shadowed already in scope | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.