Skip to content

false positive unused_parens causes syntax error around labled loops with ifs #143256

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn unused_parens

this code:

fn foo() {
    let _x = || 'outer: loop {
        let inner = 'inner: loop {
            let i = Default::default();
            if (break 'outer i) {
                loop { break 'inner 5i8; };
            } else if true {
                break 'inner 6;
            }
            break 7;
        };
        break inner < 8;
    };
}

caused the following diagnostics:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.IbV1PR0WiGNQ/icemaker_clippyfix_tempdir.vZHbFsvN0OT0/_snippet_0)
warning: unnecessary parentheses around `if` condition
 --> src/lib.rs:5:16
  |
5 |             if (break 'outer i) {
  |                ^              ^
  |
  = note: requested on the command line with `--force-warn unused-parens`
help: remove these parentheses
  |
5 -             if (break 'outer i) {
5 +             if break 'outer i {
  |

warning: `_snippet_0` (lib) generated 1 warning (run `cargo clippy --fix --lib -p _snippet_0` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

fn foo() {
    let _x = || 'outer: loop {
        let inner = 'inner: loop {
            let i = Default::default();
            if break 'outer i {
                loop { break 'inner 5i8; };
            } else if true {
                break 'inner 6;
            }
            break 7;
        };
        break inner < 8;
    };
}

no longer compiled:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.IbV1PR0WiGNQ/icemaker_clippyfix_tempdir.vZHbFsvN0OT0/_snippet_0)
error: expected identifier, found keyword `loop`
 --> src/lib.rs:6:17
  |
5 |             if break 'outer i {
  |                             - while parsing this struct
6 |                 loop { break 'inner 5i8; };
  |                 ^^^^ expected identifier, found keyword
  |
help: escape `loop` to use it as an identifier
  |
6 |                 r#loop { break 'inner 5i8; };
  |                 ++

error: this `if` expression is missing a block after the condition
 --> src/lib.rs:5:13
  |
5 |             if break 'outer i {
  |             ^^
  |
help: add a block here
 --> src/lib.rs:7:14
  |
7 |             } else if true {
  |              ^

error[E0574]: expected struct, variant or union type, found local variable `i`
 --> src/lib.rs:5:29
  |
5 |             if break 'outer i {
  |                             ^ not a struct, variant or union type

For more information about this error, try `rustc --explain E0574`.
error: could not compile `_snippet_0` (lib test) due to 3 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_0` (lib) due to 3 previous errors

Version:

rustc 1.90.0-nightly (ed2d75978 2025-06-29)
binary: rustc
commit-hash: ed2d759783dc9de134bbb3f01085b1e6dbf539f3
commit-date: 2025-06-29
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-unused_parensLint: unused_parensT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions