Skip to content

Some unused_parens suggestions can result in keyword soup (even if technically correct). #114161

Open
@eddyb

Description

@eddyb

During some testcase minimization, I expanded an a && b by hand and ended up with this:

pub fn if_and_and<T>(a: bool, b: bool, x: T, y: T) -> T {
    if (if a { b } else { false }) {
        x
    } else {
        y
    }
}

The parens make it possible to read it as "nested if, in if condition", but rustc is having none of that:

warning: unnecessary parentheses around `if` condition
 --> src/lib.rs:2:8
  |
2 |     if (if a { b } else { false }) {
  |        ^                         ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -     if (if a { b } else { false }) {
2 +     if if a { b } else { false } {
  |

I'm guessing this triggers for any combination like if (match, match (match, match (if, while (if etc.
Even if it may parse successfully, successful keywords like that is a really bad idea IMO.

Other than the expressions of the form keyword EXPR {...}, I also think e.g. return return and break break would be more readable as return (return) and break (break) (so pretty much all keyword-introduced expressions, really).

Maybe such expressions can be accidentally typed (or the result of some automated refactor?), so suggesting adding parens around them (if we had that capability) could help the user understand what rustc is even parsing their keyword soup as - but at least we shouldn't discourage them from adding the parens themselves.


Looking at other unused_parens issues it seems there's even false positive where the lint isn't even technically correct (and its suggestions wouldn't parse at all).
Maybe all the false positives should be combined into one issue? (might also help raise visibility)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-control-flowArea: Control flowA-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.L-unused_parensLint: unused_parensT-langRelevant to the language 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