Skip to content

unused_parens false positive for parenthesised Range in for loop #90807

Closed
@narpfel

Description

@narpfel

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

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.C-bugCategory: This is a bug.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