Skip to content

Commit 813f99d

Browse files
committed
Add additional refutable cases
1 parent 28cd1b8 commit 813f99d

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

clippy_lints/src/utils/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,8 @@ pub fn is_refutable(cx: &LateContext<'_, '_>, pat: &Pat) -> bool {
958958
}
959959
},
960960
PatKind::Slice(ref head, ref middle, ref tail) => {
961-
if middle.is_none() {
962-
true
963-
} else {
964-
are_refutable(cx, head.iter().chain(middle).chain(tail.iter()).map(|pat| &**pat))
965-
}
961+
// When matching slices, [..] is the only irrefutable slice pattern.
962+
!head.is_empty() || middle.is_none() || !tail.is_empty()
966963
},
967964
}
968965
}

tests/ui/while_loop.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::while_let_loop, clippy::empty_loop, clippy::while_let_on_iterator)]
22
#![allow(dead_code, clippy::never_loop, unused, clippy::cyclomatic_complexity)]
3+
#![feature(slice_patterns)]
34

45
fn main() {
56
let y = Some(true);
@@ -235,5 +236,18 @@ fn refutable() {
235236
println!("x: {}", x);
236237
println!("y: {}", y);
237238
}
239+
240+
let mut it = v.windows(2);
241+
while let Some([x, ..]) = it.next() {
242+
println!("x: {}", x);
243+
}
244+
245+
let mut it = v.windows(2);
246+
while let Some([.., y]) = it.next() {
247+
println!("y: {}", y);
248+
}
249+
250+
let mut it = v.windows(2);
251+
while let Some([..]) = it.next() {}
238252
}
239253
}

tests/ui/while_loop.stderr

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this loop could be written as a `while let` loop
2-
--> $DIR/while_loop.rs:6:5
2+
--> $DIR/while_loop.rs:7:5
33
|
44
LL | / loop {
55
LL | | if let Some(_x) = y {
@@ -13,7 +13,7 @@ LL | | }
1313
= note: `-D clippy::while-let-loop` implied by `-D warnings`
1414

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)