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

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

Closed
Kixiron opened this issue Apr 27, 2020 · 2 comments
Closed

Comments

@Kixiron
Copy link
Member

Kixiron commented Apr 27, 2020

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)
@alex-700
Copy link
Contributor

is_refutable looks buggy. For example, it thinks that a slice pattern is refutable only when it contains a refutable inner pattern. I'll deal with it by the weekends.

@flip1995
Copy link
Member

Duplicate of #3780

@alex-700 There was already a fix attempt in #3784, but was sadly abandoned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants