Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused_parens false positive with if let and ranges #121070

Closed
Jarcho opened this issue Feb 14, 2024 · 0 comments · Fixed by #121075
Closed

unused_parens false positive with if let and ranges #121070

Jarcho opened this issue Feb 14, 2024 · 0 comments · Fixed by #121075
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@Jarcho
Copy link
Contributor

Jarcho commented Feb 14, 2024

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

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 14, 2024
@chenyukang chenyukang added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 14, 2024
@chenyukang chenyukang self-assigned this Feb 14, 2024
@bors bors closed this as completed in 18c935d Feb 14, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 14, 2024
Rollup merge of rust-lang#121075 - chenyukang:yukang-fix-121070-lint-range, r=oli-obk

Fix false positive with if let and ranges

Fixes rust-lang#121070
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants