Skip to content

False positive in clippy::while_let_on_iterator #6491

@bertptrs

Description

@bertptrs

I tried this code: (simplified, the original made more sense)

fn main() {
    let mut it = 1..40;

    while let Some(n) = it.next() {
        while let Some(m) = it.next() {
            if m % 10 == 0 {
                break;
            }

            println!("doing something with m: {}", m);
        }

        println!("n still is {}", n);
    }
}

I expected to see this happen: this passes cargo clippy just fine.

Instead, this happened: it suggests an invalid improvement:

warning: this loop could be written as a `for` loop
 --> src/main.rs:5:29
  |
5 |         while let Some(m) = it.next() {
  |                             ^^^^^^^^^ help: try: `for m in it { .. }`
  |
  = note: `#[warn(clippy::while_let_on_iterator)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator

warning: 1 warning emitted

This moves out of it, which isn't allowed because it's needed in the next loop iteration.

Meta

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedT-MIRType: This lint will require working with the MIR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions