From d658bcfdfe57c41a63e3826ebc12c49f4c77008c Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 10 Oct 2025 12:24:15 +0800 Subject: [PATCH] Suppress unused_parens for labeled break --- compiler/rustc_lint/src/unused.rs | 11 +++++++++- .../unused/break-label-with-parens-147542.rs | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/ui/lint/unused/break-label-with-parens-147542.rs diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 874c435402919..1594a0361a62c 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -914,7 +914,16 @@ trait UnusedDelimLint { (value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true) } - Break(_, Some(ref value)) => { + Break(label, Some(ref value)) => { + // Don't lint on `break 'label ({...})` - the parens are necessary + // to disambiguate from `break 'label {...}` which would be a syntax error. + // This avoids conflicts with the `break_with_label_and_loop` lint. + if label.is_some() + && matches!(value.kind, ast::ExprKind::Paren(ref inner) + if matches!(inner.kind, ast::ExprKind::Block(..))) + { + return; + } (value, UnusedDelimsCtx::BreakValue, false, None, None, true) } diff --git a/tests/ui/lint/unused/break-label-with-parens-147542.rs b/tests/ui/lint/unused/break-label-with-parens-147542.rs new file mode 100644 index 0000000000000..227a00ad40882 --- /dev/null +++ b/tests/ui/lint/unused/break-label-with-parens-147542.rs @@ -0,0 +1,22 @@ +//@ check-pass + +// Regression test for #147542 +// Ensures that we don't suggest removing parens in a break with label and loop +// when the parens are necessary for correct parsing. + +#![warn(unused_parens)] +#![warn(break_with_label_and_loop)] + +fn xyz() -> usize { + 'foo: { + // parens bellow are necessary break of break with label and loop + break 'foo ({ + println!("Hello!"); + 123 + }); + } +} + +fn main() { + xyz(); +}