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 for parenthesised Range in for loop #90807

Closed
narpfel opened this issue Nov 11, 2021 · 0 comments · Fixed by #91956
Closed

unused_parens false positive for parenthesised Range in for loop #90807

narpfel opened this issue Nov 11, 2021 · 0 comments · Fixed by #91956
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@narpfel
Copy link
Contributor

narpfel commented Nov 11, 2021

Given the following code: playground stable / nightly

fn main() {
    for _ in (1..{ 2 }) {}
}

The current output is:

   Compiling playground v0.0.1 (/playground)
warning: unnecessary parentheses around `for` iterator expression
 --> src/main.rs:2:14
  |
2 |     for _ in (1..{ 2 }) {}
  |              ^        ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -     for _ in (1..{ 2 }) {}
2 +     for _ in 1..{ 2 } {}
  | 

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

No warnings should be generated as removing the parentheses around the Range expression changes the meaning of the code (to a compiler error):

fn main() {
    for _ in 1..{ 2 } {}
}

The suggested code is parsed as a RangeFrom and { 2 } as the for loop’s block, followed by en empty block:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:19
  |
2 |     for _ in 1..{ 2 } {}
  |                   ^ expected `()`, found integer

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

playground


rustc versions

  • stable 1.56.1
  • nightly (installed via rustup)
$ rustc --version --verbose
rustc 1.58.0-nightly (8b09ba6a5 2021-11-09)
binary: rustc
commit-hash: 8b09ba6a5d5c644fe0f1c27c7f9c80b334241707
commit-date: 2021-11-09
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
@narpfel narpfel added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 11, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 18, 2021
…ange, r=nagisa

fix(rustc_lint): better detect when parens are necessary

Fixes rust-lang#90807
@camelid camelid added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Dec 19, 2021
@bors bors closed this as completed in 6b7fcf7 Dec 19, 2021
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. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants