Rollup merge of #151811 - chenyukang:yukang-fix-143256-unused-parens-labeled-loops, r=Kivooeo

Fix false positive in unused_parens caused by break

Fixes rust-lang/rust#143256
This commit is contained in:
Stuart Cook 2026-01-30 17:41:07 +11:00 committed by GitHub
commit 45009749b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View file

@ -805,7 +805,10 @@ trait UnusedDelimLint {
ExprKind::Break(_label, None) => return false,
ExprKind::Break(_label, Some(break_expr)) => {
return matches!(break_expr.kind, ExprKind::Block(..));
// `if (break 'label i) { ... }` removing parens would make `i { ... }`
// be parsed as a struct literal, so keep parentheses if the break value
// ends with a path (which could be mistaken for a struct name).
return matches!(break_expr.kind, ExprKind::Block(..) | ExprKind::Path(..));
}
ExprKind::Range(_lhs, Some(rhs), _limits) => {

View file

@ -0,0 +1,25 @@
//@ check-pass
// testcase for https://github.com/rust-lang/rust/issues/143256
#![deny(unused_parens)]
#![allow(unreachable_code, unused_variables, dead_code)]
fn foo() {
let _x = || 'outer: loop {
let inner = 'inner: loop {
let i = Default::default();
// the parentheses here are necessary
if (break 'outer i) {
loop {
break 'inner 5i8;
}
} else if true {
break 'inner 6;
}
break 7;
};
break inner < 8;
};
}
fn main() {}