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

Fix invalid while_let_on_iterator suggestion #3784

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,16 @@ pub fn is_refutable(cx: &LateContext<'_, '_>, pat: &Pat) -> bool {
}
},
PatKind::Slice(ref head, ref middle, ref tail) => {
are_refutable(cx, head.iter().chain(middle).chain(tail.iter()).map(|pat| &**pat))
match &cx.tables.node_type(pat.hir_id).sty {
ty::Slice(..) => {
// [..] is the only irrefutable slice pattern.
!head.is_empty() || middle.is_none() || !tail.is_empty()
},
_ => {
// Assume it's an array.
are_refutable(cx, head.iter().chain(middle).chain(tail.iter()).map(|pat| &**pat))
},
}
},
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/while_loop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(clippy::while_let_loop, clippy::empty_loop, clippy::while_let_on_iterator)]
#![allow(dead_code, clippy::never_loop, unused, clippy::cyclomatic_complexity)]
#![feature(slice_patterns)]

fn main() {
let y = Some(true);
Expand Down Expand Up @@ -226,4 +227,34 @@ fn refutable() {
let _ = elem.or_else(|| *iter.next()?);
}
}

// Issue 3780
{
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);
}

let mut it = v.windows(2);
while let Some([x, ..]) = it.next() {
println!("x: {}", x);
}

let mut it = v.windows(2);
while let Some([.., y]) = it.next() {
println!("y: {}", y);
}

let mut it = v.windows(2);
while let Some([..]) = it.next() {}

let v = vec![[1], [2], [3]];
let mut it = v.iter();
while let Some([1]) = it.next() {}

let mut it = v.iter();
while let Some([x]) = it.next() {}
}
}
38 changes: 25 additions & 13 deletions tests/ui/while_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this loop could be written as a `while let` loop
--> $DIR/while_loop.rs:6:5
--> $DIR/while_loop.rs:7:5
|
LL | / loop {
LL | | if let Some(_x) = y {
Expand All @@ -13,7 +13,7 @@ LL | | }
= note: `-D clippy::while-let-loop` implied by `-D warnings`

error: this loop could be written as a `while let` loop
--> $DIR/while_loop.rs:20:5
--> $DIR/while_loop.rs:21:5
|
LL | / loop {
LL | | match y {
Expand All @@ -24,7 +24,7 @@ LL | | }
| |_____^ help: try: `while let Some(_x) = y { .. }`

error: this loop could be written as a `while let` loop
--> $DIR/while_loop.rs:26:5
--> $DIR/while_loop.rs:27:5
|
LL | / loop {
LL | | let x = match y {
Expand All @@ -36,7 +36,7 @@ LL | | }
| |_____^ help: try: `while let Some(x) = y { .. }`

error: this loop could be written as a `while let` loop
--> $DIR/while_loop.rs:34:5
--> $DIR/while_loop.rs:35:5
|
LL | / loop {
LL | | let x = match y {
Expand All @@ -48,7 +48,7 @@ LL | | }
| |_____^ help: try: `while let Some(x) = y { .. }`

error: this loop could be written as a `while let` loop
--> $DIR/while_loop.rs:62:5
--> $DIR/while_loop.rs:63:5
|
LL | / loop {
LL | | let (e, l) = match "".split_whitespace().next() {
Expand All @@ -60,27 +60,27 @@ LL | | }
| |_____^ help: try: `while let Some(word) = "".split_whitespace().next() { .. }`

error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:72:33
--> $DIR/while_loop.rs:73:33
|
LL | while let Option::Some(x) = iter.next() {
| ^^^^^^^^^^^ help: try: `for x in iter { .. }`
|
= note: `-D clippy::while-let-on-iterator` implied by `-D warnings`

error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:77:25
--> $DIR/while_loop.rs:78:25
|
LL | while let Some(x) = iter.next() {
| ^^^^^^^^^^^ help: try: `for x in iter { .. }`

error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:82:25
--> $DIR/while_loop.rs:83:25
|
LL | while let Some(_) = iter.next() {}
| ^^^^^^^^^^^ help: try: `for _ in iter { .. }`

error: this loop could be written as a `while let` loop
--> $DIR/while_loop.rs:125:5
--> $DIR/while_loop.rs:126:5
|
LL | / loop {
LL | | let _ = match iter.next() {
Expand All @@ -92,24 +92,36 @@ LL | | }
| |_____^ help: try: `while let Some(ele) = iter.next() { .. }`

error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
--> $DIR/while_loop.rs:130:9
--> $DIR/while_loop.rs:131:9
|
LL | loop {}
| ^^^^^^^
|
= note: `-D clippy::empty-loop` implied by `-D warnings`

error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:188:29
--> $DIR/while_loop.rs:189:29
|
LL | while let Some(v) = y.next() {
| ^^^^^^^^ help: try: `for v in y { .. }`

error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:216:26
--> $DIR/while_loop.rs:217:26
|
LL | while let Some(..) = values.iter().next() {
| ^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter() { .. }`

error: aborting due to 12 previous errors
error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:251:32
|
LL | while let Some([..]) = it.next() {}
| ^^^^^^^^^ help: try: `for [..] in it { .. }`

error: this loop could be written as a `for` loop
--> $DIR/while_loop.rs:258:31
|
LL | while let Some([x]) = it.next() {}
| ^^^^^^^^^ help: try: `for [x] in it { .. }`

error: aborting due to 14 previous errors