Skip to content

Bad while_let_on_iterator suggestion when pattern is refutable #3780

Closed
@ghost

Description

Here is an example:

fn main() {
    let v = vec![1, 2, 3];
    
    {
        let mut it = v.windows(2);
        while let Some([x, y]) = it.next() {
            println!("x: {}", x);
            println!("y: {}", y);
        }
    }
    /*
    warning: this loop could be written as a `for` loop
     --> src/main.rs:6:34
      |
    6 |         while let Some([x, y]) = it.next() {
      |                                  ^^^^^^^^^ help: try: `for [x, y] in it { .. }`
      |
      = note: #[warn(clippy::while_let_on_iterator)] on by default
    */
    
    // The Clippy suggestion fails to compile
    /*
    {
        let mut it = v.windows(2);
        for [x, y] in it {
            println!("x: {}", x);
            println!("y: {}", y);
        }
    }
    */
}

Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions