Skip to content

Commit d658bcf

Browse files
committed
Suppress unused_parens for labeled break
1 parent 9725c4b commit d658bcf

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,16 @@ trait UnusedDelimLint {
914914
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true)
915915
}
916916

917-
Break(_, Some(ref value)) => {
917+
Break(label, Some(ref value)) => {
918+
// Don't lint on `break 'label ({...})` - the parens are necessary
919+
// to disambiguate from `break 'label {...}` which would be a syntax error.
920+
// This avoids conflicts with the `break_with_label_and_loop` lint.
921+
if label.is_some()
922+
&& matches!(value.kind, ast::ExprKind::Paren(ref inner)
923+
if matches!(inner.kind, ast::ExprKind::Block(..)))
924+
{
925+
return;
926+
}
918927
(value, UnusedDelimsCtx::BreakValue, false, None, None, true)
919928
}
920929

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ check-pass
2+
3+
// Regression test for #147542
4+
// Ensures that we don't suggest removing parens in a break with label and loop
5+
// when the parens are necessary for correct parsing.
6+
7+
#![warn(unused_parens)]
8+
#![warn(break_with_label_and_loop)]
9+
10+
fn xyz() -> usize {
11+
'foo: {
12+
// parens bellow are necessary break of break with label and loop
13+
break 'foo ({
14+
println!("Hello!");
15+
123
16+
});
17+
}
18+
}
19+
20+
fn main() {
21+
xyz();
22+
}

0 commit comments

Comments
 (0)