Skip to content

false positive for unused_parens with binary op if rhs needs parens #110251

Closed
@lukas-code

Description

@lukas-code

I tried this very normal code: link

#![allow(unreachable_code)]

fn main() {
    if (() = return) {}
    if ((..{}) == ..{}) {}
}

The current output is the following, but removing any of the parens is an error:

   Compiling playground v0.0.1 (/playground)
warning: unnecessary parentheses around `if` condition
 --> src/main.rs:4:8
  |
4 |     if (() = return) {}
  |        ^           ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
4 -     if (() = return) {}
4 +     if () = return {}
  |

warning: unnecessary parentheses around `if` condition
 --> src/main.rs:5:8
  |
5 |     if ((..{}) == ..{}) {}
  |        ^              ^
  |
help: remove these parentheses
  |
5 -     if ((..{}) == ..{}) {}
5 +     if (..{}) == ..{} {}
  |

warning: `playground` (bin "playground") generated 2 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.99s
     Running `target/debug/playground`

This also gets cargo fixed incorrectly.

This need to change to look at the rhs of binops:

|| (followed_by_block
&& match &inner.kind {
ExprKind::Ret(_)
| ExprKind::Break(..)
| ExprKind::Yield(..)
| ExprKind::Yeet(..) => true,
ExprKind::Range(_lhs, Some(rhs), _limits) => {
matches!(rhs.kind, ExprKind::Block(..))
}
_ => parser::contains_exterior_struct_lit(&inner),
})

@rustbot label T-compiler A-lint A-diagnostics

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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