Ignore instructions following a break from block in never_loop lint

It is not sufficient to ignore break from a block inside the loop.
Instructions after the break must be ignored, as they are unreachable.
This is also true for all instructions in outer blocks and loops
until the right block is reached.
This commit is contained in:
Samuel Tardieu 2023-02-13 20:37:36 +01:00
parent e9dffa3910
commit 657ee48bec
3 changed files with 78 additions and 10 deletions

View file

@ -253,12 +253,48 @@ pub fn test20() {
pub fn test21() {
loop {
'a: {
{ }
{}
break 'a;
}
}
}
// Issue 10304: code after break from block was not considered
// unreachable code and was considered for further analysis of
// whether the loop would ever be executed or not.
pub fn test22() {
for _ in 0..10 {
'block: {
break 'block;
return;
}
println!("looped");
}
}
pub fn test23() {
for _ in 0..10 {
'block: {
for _ in 0..20 {
break 'block;
}
}
println!("looped");
}
}
pub fn test24() {
'a: for _ in 0..10 {
'b: {
let x = Some(1);
match x {
None => break 'a,
Some(_) => break 'b,
}
}
}
}
fn main() {
test1();
test2();

View file

@ -126,5 +126,18 @@ LL | | }
LL | | }
| |_____^
error: aborting due to 11 previous errors
error: this loop never actually loops
--> $DIR/never_loop.rs:278:13
|
LL | / for _ in 0..20 {
LL | | break 'block;
LL | | }
| |_____________^
|
help: if you need the first element of the iterator, try writing
|
LL | if let Some(_) = (0..20).next() {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 12 previous errors