Skip to content

Clippy reports a while_let_on_iterator with example code that does not compile #5540

Closed
@Kixiron

Description

@Kixiron

Bug

Clippy reports a while_let_on_iterator with example code that does not compile. Playground link

Original code

let mut slice = [1, 2, 3, 4, 5, 6, 7, 8].windows(2);
while let Some([a, b]) = slice.next() {
    println!("{}, {}", a, b);
}

Clippy Error

warning: this loop could be written as a `for` loop
 --> src/main.rs:4:30
  |
4 |     while let Some([a, b]) = slice.next() {
  |                              ^^^^^^^^^^^^ help: try: `for [a, b] in slice { .. }`
  |
  = 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

Code with clippy solution

let mut slice = [1, 2, 3, 4, 5, 6, 7, 8].windows(2);
for [a, b] in slice {
    println!("{}, {}", a, b);
}

Compile Error

error[E0005]: refutable pattern in `for` loop binding: `&[]`, `&[_]` and `&[_, _, _, ..]` not covered
  --> src/main.rs:10:9
   |
10 |     for [a, b] in slice {
   |         ^^^^^^ patterns `&[]`, `&[_]` and `&[_, _, _, ..]` not covered

Clippy Version

❯ cargo clippy -V
clippy 0.0.212 (6651c1b9 2020-04-15)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions