Skip to content

unused_parens false positive with if let and ranges #121070

Closed
@Jarcho

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

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