Skip to content

unused_parens false positive with if let and ranges #121070

Closed

Description

@Jarcho

Range expressions have a lower precedence than let expression. e.g.

fn main() {
    if let x = (0..1) {}
}
warning: unnecessary parentheses around `let` scrutinee expression
 --> src/main.rs:2:16
  |
2 |     if let x = (0..1) {}
  |                ^    ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -     if let x = (0..1) {}
2 +     if let x = 0..1 {}
  |

Not using the parenthesis as suggested

fn main() {
    if let x = 0..1 {}
}
error: expected expression, found `let` statement
 --> src/main.rs:2:8
  |
2 |     if let x = 0..1 {}
  |        ^^^^^^^^^
  |
  = note: only supported directly in conditions of `if` and `while` expressions

It would be better if let expressions just had a lower precedence, but that wouldn't be compatible with the fact the ranges are also lower than && and ||. The following:

fn main() {
    println!("{:?}", true && false .. false || true);
}

Prints false..true.


Tested on nightly-2024-02-13

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 14, 2024
  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 14, 2024
  3. self-assigned this
    on Feb 14, 2024
  4. added a commit that references this issue on Feb 14, 2024
    18c935d
  5. added a commit that references this issue on Feb 14, 2024
    46d5e8b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions