-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add lint checks for unused loop labels #50763
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d67628e
Add lint checks for unused loop labels
kylestach 7c4aa73
Rename test to `unused_label` and remove empty `stdout` file
kylestach bb867d3
Add additional test case to unused_label lint
kylestach acd6ab8
Rename `unused_loop_label` to `unused_label` and fix/clean up lint logic
kylestach 0f27412
Add test case for shadowed labels, with the inner broken multiple times
kylestach 4de4e61
Fix ignored unused outer label when inner label shadows and is broken…
kylestach 167cede
Fix formatting in unused label linting to make tidy checks pass
kylestach 7676982
Revert "Add lint checks for unused loop labels"
kylestach 88f4063
Reimplement unused_labels lint as a compiler builtin in the resolver
kylestach 702f1dd
Add tests for new labeled blocks for `unused_labels`
kylestach f488d2b
Remove unused label in mir
kylestach a336aa9
Allow `unused_labels` in some compile-fail tests
kylestach 335f91f
Revert "Allow `unused_labels` in some compile-fail tests"
kylestach 5586cd8
Revert "Remove unused label in mir"
kylestach 6da64a7
Default `unused_labels` to allow, move to "unused"
kylestach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before merging, can you also add a test case for the following, which should be allowed?