Skip to content

False positive unused_braces #141783

@matthiaskrgr

Description

@matthiaskrgr

I tried this code:

#[allow(unreachable_code)]
#[warn(unused_braces)]

fn main() {
    if return { return } { return } else { return }
}
warning: unnecessary braces around `return` value
 --> a.rs:3:15
  |
3 |     if return { return } { return } else { return }
  |               ^^      ^^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
3 -     if return { return } { return } else { return }
3 +     if return return { return } else { return }
  |

warning: 1 warning emitted

the suggestion does not compile:

#[allow(unreachable_code)]
#[warn(unused_braces)]

fn main() {
    if return return { return } else { return }
}

=>

error: this `if` expression is missing a block after the condition
 --> a.rs:5:5
  |
5 |     if return return { return } else { return }
  |     ^^
  |
help: add a block here
 --> a.rs:5:32
  |
5 |     if return return { return } else { return }
  |                                ^

warning: unnecessary braces around `return` value
 --> a.rs:5:22
  |
5 |     if return return { return } else { return }
  |                      ^^      ^^
  |
note: the lint level is defined here
 --> a.rs:2:8
  |
2 | #[warn(unused_braces)]
  |        ^^^^^^^^^^^^^
help: remove these braces
  |
5 -     if return return { return } else { return }
5 +     if return return return else { return }
  |

error: aborting due to 1 previous error; 1 warning emitted
rustc 1.89.0-nightly (1bbd62e54 2025-05-29)
binary: rustc
commit-hash: 1bbd62e547ba5cc08ccb44c27def3d33195d2dd5
commit-date: 2025-05-29
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-false-positiveLint: False positive (should not have fired).L-unused_bracesLint: unused_bracesT-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