Skip to content

Commit d056ea6

Browse files
committed
Auto merge of #4069 - mikerite:while_loop_test_split, r=phansch
Reorganize "while loop" tests cc #2038 changelog: none
2 parents 341c96a + dcfe380 commit d056ea6

7 files changed

+254
-240
lines changed

tests/ui/ice-360.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {}
2+
3+
fn no_panic<T>(slice: &[T]) {
4+
let mut iter = slice.iter();
5+
loop {
6+
let _ = match iter.next() {
7+
Some(ele) => ele,
8+
None => break,
9+
};
10+
loop {}
11+
}
12+
}

tests/ui/ice-360.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: this loop could be written as a `while let` loop
2+
--> $DIR/ice-360.rs:5:5
3+
|
4+
LL | / loop {
5+
LL | | let _ = match iter.next() {
6+
LL | | Some(ele) => ele,
7+
LL | | None => break,
8+
LL | | };
9+
LL | | loop {}
10+
LL | | }
11+
| |_____^ help: try: `while let Some(ele) = iter.next() { .. }`
12+
|
13+
= note: `-D clippy::while-let-loop` implied by `-D warnings`
14+
15+
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
16+
--> $DIR/ice-360.rs:10:9
17+
|
18+
LL | loop {}
19+
| ^^^^^^^
20+
|
21+
= note: `-D clippy::empty-loop` implied by `-D warnings`
22+
23+
error: aborting due to 2 previous errors
24+

tests/ui/while_let_loop.rs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#![warn(clippy::while_let_loop)]
2+
3+
fn main() {
4+
let y = Some(true);
5+
loop {
6+
if let Some(_x) = y {
7+
let _v = 1;
8+
} else {
9+
break;
10+
}
11+
}
12+
13+
#[allow(clippy::never_loop)]
14+
loop {
15+
// no error, break is not in else clause
16+
if let Some(_x) = y {
17+
let _v = 1;
18+
}
19+
break;
20+
}
21+
22+
loop {
23+
match y {
24+
Some(_x) => true,
25+
None => break,
26+
};
27+
}
28+
29+
loop {
30+
let x = match y {
31+
Some(x) => x,
32+
None => break,
33+
};
34+
let _x = x;
35+
let _str = "foo";
36+
}
37+
38+
loop {
39+
let x = match y {
40+
Some(x) => x,
41+
None => break,
42+
};
43+
{
44+
let _a = "bar";
45+
};
46+
{
47+
let _b = "foobar";
48+
}
49+
}
50+
51+
loop {
52+
// no error, else branch does something other than break
53+
match y {
54+
Some(_x) => true,
55+
_ => {
56+
let _z = 1;
57+
break;
58+
},
59+
};
60+
}
61+
62+
while let Some(x) = y {
63+
// no error, obviously
64+
println!("{}", x);
65+
}
66+
67+
// #675, this used to have a wrong suggestion
68+
loop {
69+
let (e, l) = match "".split_whitespace().next() {
70+
Some(word) => (word.is_empty(), word.len()),
71+
None => break,
72+
};
73+
74+
let _ = (e, l);
75+
}
76+
}
77+
78+
fn issue771() {
79+
let mut a = 100;
80+
let b = Some(true);
81+
loop {
82+
if a > 10 {
83+
break;
84+
}
85+
86+
match b {
87+
Some(_) => a = 0,
88+
None => break,
89+
}
90+
}
91+
}
92+
93+
fn issue1017() {
94+
let r: Result<u32, u32> = Ok(42);
95+
let mut len = 1337;
96+
97+
loop {
98+
match r {
99+
Err(_) => len = 0,
100+
Ok(length) => {
101+
len = length;
102+
break;
103+
},
104+
}
105+
}
106+
}
107+
108+
#[allow(clippy::never_loop)]
109+
fn issue1948() {
110+
// should not trigger clippy::while_let_loop lint because break passes an expression
111+
let a = Some(10);
112+
let b = loop {
113+
if let Some(c) = a {
114+
break Some(c);
115+
} else {
116+
break None;
117+
}
118+
};
119+
}

tests/ui/while_let_loop.stderr

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error: this loop could be written as a `while let` loop
2+
--> $DIR/while_let_loop.rs:5:5
3+
|
4+
LL | / loop {
5+
LL | | if let Some(_x) = y {
6+
LL | | let _v = 1;
7+
LL | | } else {
8+
LL | | break;
9+
LL | | }
10+
LL | | }
11+
| |_____^ help: try: `while let Some(_x) = y { .. }`
12+
|
13+
= note: `-D clippy::while-let-loop` implied by `-D warnings`
14+
15+
error: this loop could be written as a `while let` loop
16+
--> $DIR/while_let_loop.rs:22:5
17+
|
18+
LL | / loop {
19+
LL | | match y {
20+
LL | | Some(_x) => true,
21+
LL | | None => break,
22+
LL | | };
23+
LL | | }
24+
| |_____^ help: try: `while let Some(_x) = y { .. }`
25+
26+
error: this loop could be written as a `while let` loop
27+
--> $DIR/while_let_loop.rs:29:5
28+
|
29+
LL | / loop {
30+
LL | | let x = match y {
31+
LL | | Some(x) => x,
32+
LL | | None => break,
33+
... |
34+
LL | | let _str = "foo";
35+
LL | | }
36+
| |_____^ help: try: `while let Some(x) = y { .. }`
37+
38+
error: this loop could be written as a `while let` loop
39+
--> $DIR/while_let_loop.rs:38:5
40+
|
41+
LL | / loop {
42+
LL | | let x = match y {
43+
LL | | Some(x) => x,
44+
LL | | None => break,
45+
... |
46+
LL | | }
47+
LL | | }
48+
| |_____^ help: try: `while let Some(x) = y { .. }`
49+
50+
error: this loop could be written as a `while let` loop
51+
--> $DIR/while_let_loop.rs:68:5
52+
|
53+
LL | / loop {
54+
LL | | let (e, l) = match "".split_whitespace().next() {
55+
LL | | Some(word) => (word.is_empty(), word.len()),
56+
LL | | None => break,
57+
... |
58+
LL | | let _ = (e, l);
59+
LL | | }
60+
| |_____^ help: try: `while let Some(word) = "".split_whitespace().next() { .. }`
61+
62+
error: aborting due to 5 previous errors
63+
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,7 @@
1-
#![warn(clippy::while_let_loop, clippy::empty_loop, clippy::while_let_on_iterator)]
2-
#![allow(dead_code, clippy::never_loop, unused, clippy::cognitive_complexity)]
1+
#![warn(clippy::while_let_on_iterator)]
2+
#![allow(clippy::never_loop, clippy::cognitive_complexity)]
33

44
fn main() {
5-
let y = Some(true);
6-
loop {
7-
if let Some(_x) = y {
8-
let _v = 1;
9-
} else {
10-
break;
11-
}
12-
}
13-
loop {
14-
// no error, break is not in else clause
15-
if let Some(_x) = y {
16-
let _v = 1;
17-
}
18-
break;
19-
}
20-
loop {
21-
match y {
22-
Some(_x) => true,
23-
None => break,
24-
};
25-
}
26-
loop {
27-
let x = match y {
28-
Some(x) => x,
29-
None => break,
30-
};
31-
let _x = x;
32-
let _str = "foo";
33-
}
34-
loop {
35-
let x = match y {
36-
Some(x) => x,
37-
None => break,
38-
};
39-
{
40-
let _a = "bar";
41-
};
42-
{
43-
let _b = "foobar";
44-
}
45-
}
46-
loop {
47-
// no error, else branch does something other than break
48-
match y {
49-
Some(_x) => true,
50-
_ => {
51-
let _z = 1;
52-
break;
53-
},
54-
};
55-
}
56-
while let Some(x) = y {
57-
// no error, obviously
58-
println!("{}", x);
59-
}
60-
61-
// #675, this used to have a wrong suggestion
62-
loop {
63-
let (e, l) = match "".split_whitespace().next() {
64-
Some(word) => (word.is_empty(), word.len()),
65-
None => break,
66-
};
67-
68-
let _ = (e, l);
69-
}
70-
715
let mut iter = 1..20;
726
while let Option::Some(x) = iter.next() {
737
println!("{}", x);
@@ -116,36 +50,6 @@ fn main() {
11650
}
11751
}
11852

119-
// regression test (#360)
120-
// this should not panic
121-
// it's ok if further iterations of the lint
122-
// cause this function to trigger it
123-
fn no_panic<T>(slice: &[T]) {
124-
let mut iter = slice.iter();
125-
loop {
126-
let _ = match iter.next() {
127-
Some(ele) => ele,
128-
None => break,
129-
};
130-
loop {}
131-
}
132-
}
133-
134-
fn issue1017() {
135-
let r: Result<u32, u32> = Ok(42);
136-
let mut len = 1337;
137-
138-
loop {
139-
match r {
140-
Err(_) => len = 0,
141-
Ok(length) => {
142-
len = length;
143-
break;
144-
},
145-
}
146-
}
147-
}
148-
14953
// Issue #1188
15054
fn refutable() {
15155
let a = [42, 1337];
@@ -194,18 +98,6 @@ fn nested_loops() {
19498
}
19599
}
196100

197-
fn issue1948() {
198-
// should not trigger clippy::while_let_loop lint because break passes an expression
199-
let a = Some(10);
200-
let b = loop {
201-
if let Some(c) = a {
202-
break Some(c);
203-
} else {
204-
break None;
205-
}
206-
};
207-
}
208-
209101
fn issue1121() {
210102
use std::collections::HashSet;
211103
let mut values = HashSet::new();
@@ -238,18 +130,3 @@ fn issue3670() {
238130
let _ = elem.or_else(|| *iter.next()?);
239131
}
240132
}
241-
242-
fn issue771() {
243-
let mut a = 100;
244-
let b = Some(true);
245-
loop {
246-
if a > 10 {
247-
break;
248-
}
249-
250-
match b {
251-
Some(_) => a = 0,
252-
None => break,
253-
}
254-
}
255-
}

0 commit comments

Comments
 (0)