Skip to content

Commit 68ac097

Browse files
authored
Unrolled build for #151811
Rollup merge of #151811 - chenyukang:yukang-fix-143256-unused-parens-labeled-loops, r=Kivooeo Fix false positive in unused_parens caused by break Fixes #143256
2 parents ef2657c + 4803644 commit 68ac097

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,10 @@ trait UnusedDelimLint {
805805

806806
ExprKind::Break(_label, None) => return false,
807807
ExprKind::Break(_label, Some(break_expr)) => {
808-
return matches!(break_expr.kind, ExprKind::Block(..));
808+
// `if (break 'label i) { ... }` removing parens would make `i { ... }`
809+
// be parsed as a struct literal, so keep parentheses if the break value
810+
// ends with a path (which could be mistaken for a struct name).
811+
return matches!(break_expr.kind, ExprKind::Block(..) | ExprKind::Path(..));
809812
}
810813

811814
ExprKind::Range(_lhs, Some(rhs), _limits) => {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ check-pass
2+
// testcase for https://github.com/rust-lang/rust/issues/143256
3+
4+
#![deny(unused_parens)]
5+
#![allow(unreachable_code, unused_variables, dead_code)]
6+
7+
fn foo() {
8+
let _x = || 'outer: loop {
9+
let inner = 'inner: loop {
10+
let i = Default::default();
11+
// the parentheses here are necessary
12+
if (break 'outer i) {
13+
loop {
14+
break 'inner 5i8;
15+
}
16+
} else if true {
17+
break 'inner 6;
18+
}
19+
break 7;
20+
};
21+
break inner < 8;
22+
};
23+
}
24+
25+
fn main() {}

0 commit comments

Comments
 (0)