Do not ignore statements before a break when simplifying loop

The previous tests ignored statements in a block and only looked at the
final expression to determine if the block was made of a single `break`.
This commit is contained in:
Samuel Tardieu
2026-01-11 09:06:44 +01:00
parent bcd52c1150
commit 6fa61dfd99
2 changed files with 39 additions and 12 deletions
+21
View File
@@ -253,3 +253,24 @@ fn let_assign() {
}
}
}
fn issue16378() {
// This does not lint today because of the extra statement(s)
// before the `break`.
// TODO: When the `break` statement/expr in the `let`/`else` is the
// only way to leave the loop, the lint could trigger and move
// the statements preceeding the `break` after the loop, as in:
// ```rust
// while let Some(x) = std::hint::black_box(None::<i32>) {
// println!("x = {x}");
// }
// println!("fail");
// ```
loop {
let Some(x) = std::hint::black_box(None::<i32>) else {
println!("fail");
break;
};
println!("x = {x}");
}
}