Skip to content

false-negative unused_parens in closures consisting of just one expression #136741

@ada4a

Description

@ada4a

Code

pub fn main() {
    let _ = || (0 == 0);
}

Current output

<compiles without warnings>

Desired output

warning: unnecessary parentheses around block return value
 --> foo.rs:2:16
  |
2 |         (0 == 0)
  |         ^      ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -    let _ = || (0 == 0);
2 +    let _ = || 0 == 0;
  |

warning: 1 warning emitted

Rationale and extra context

Note that the code above is really the minimal example, where not having any delimiters could actually be confusing (but one could use braces). What I had initially is a closure inside an iterator combinator, something like this:

let _ = (0..).find(|n| (n % 2 == 0));

Other cases

pub fn main() {
    let _ = || {
        _ = 0;
        (0 == 0)
    };
}

Rust Version

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-unknown-linux-gnu
release: 1.84.1
LLVM version: 19.1.5

Anything else?

Also tested on the latest nightly (rustc 1.86.0-nightly (a9e7b3048 2025-02-07))

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.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