mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
Auto merge of #4069 - mikerite:while_loop_test_split, r=phansch
Reorganize "while loop" tests cc #2038 changelog: none
This commit is contained in:
commit
d056ea6828
7 changed files with 254 additions and 240 deletions
12
tests/ui/ice-360.rs
Normal file
12
tests/ui/ice-360.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
fn main() {}
|
||||
|
||||
fn no_panic<T>(slice: &[T]) {
|
||||
let mut iter = slice.iter();
|
||||
loop {
|
||||
let _ = match iter.next() {
|
||||
Some(ele) => ele,
|
||||
None => break,
|
||||
};
|
||||
loop {}
|
||||
}
|
||||
}
|
24
tests/ui/ice-360.stderr
Normal file
24
tests/ui/ice-360.stderr
Normal file
|
@ -0,0 +1,24 @@
|
|||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/ice-360.rs:5:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let _ = match iter.next() {
|
||||
LL | | Some(ele) => ele,
|
||||
LL | | None => break,
|
||||
LL | | };
|
||||
LL | | loop {}
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(ele) = iter.next() { .. }`
|
||||
|
|
||||
= note: `-D clippy::while-let-loop` implied by `-D warnings`
|
||||
|
||||
error: empty `loop {}` detected. You may want to either use `panic!()` or add `std::thread::sleep(..);` to the loop body.
|
||||
--> $DIR/ice-360.rs:10:9
|
||||
|
|
||||
LL | loop {}
|
||||
| ^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::empty-loop` implied by `-D warnings`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
119
tests/ui/while_let_loop.rs
Normal file
119
tests/ui/while_let_loop.rs
Normal file
|
@ -0,0 +1,119 @@
|
|||
#![warn(clippy::while_let_loop)]
|
||||
|
||||
fn main() {
|
||||
let y = Some(true);
|
||||
loop {
|
||||
if let Some(_x) = y {
|
||||
let _v = 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::never_loop)]
|
||||
loop {
|
||||
// no error, break is not in else clause
|
||||
if let Some(_x) = y {
|
||||
let _v = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
loop {
|
||||
match y {
|
||||
Some(_x) => true,
|
||||
None => break,
|
||||
};
|
||||
}
|
||||
|
||||
loop {
|
||||
let x = match y {
|
||||
Some(x) => x,
|
||||
None => break,
|
||||
};
|
||||
let _x = x;
|
||||
let _str = "foo";
|
||||
}
|
||||
|
||||
loop {
|
||||
let x = match y {
|
||||
Some(x) => x,
|
||||
None => break,
|
||||
};
|
||||
{
|
||||
let _a = "bar";
|
||||
};
|
||||
{
|
||||
let _b = "foobar";
|
||||
}
|
||||
}
|
||||
|
||||
loop {
|
||||
// no error, else branch does something other than break
|
||||
match y {
|
||||
Some(_x) => true,
|
||||
_ => {
|
||||
let _z = 1;
|
||||
break;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
while let Some(x) = y {
|
||||
// no error, obviously
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// #675, this used to have a wrong suggestion
|
||||
loop {
|
||||
let (e, l) = match "".split_whitespace().next() {
|
||||
Some(word) => (word.is_empty(), word.len()),
|
||||
None => break,
|
||||
};
|
||||
|
||||
let _ = (e, l);
|
||||
}
|
||||
}
|
||||
|
||||
fn issue771() {
|
||||
let mut a = 100;
|
||||
let b = Some(true);
|
||||
loop {
|
||||
if a > 10 {
|
||||
break;
|
||||
}
|
||||
|
||||
match b {
|
||||
Some(_) => a = 0,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn issue1017() {
|
||||
let r: Result<u32, u32> = Ok(42);
|
||||
let mut len = 1337;
|
||||
|
||||
loop {
|
||||
match r {
|
||||
Err(_) => len = 0,
|
||||
Ok(length) => {
|
||||
len = length;
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::never_loop)]
|
||||
fn issue1948() {
|
||||
// should not trigger clippy::while_let_loop lint because break passes an expression
|
||||
let a = Some(10);
|
||||
let b = loop {
|
||||
if let Some(c) = a {
|
||||
break Some(c);
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
};
|
||||
}
|
63
tests/ui/while_let_loop.stderr
Normal file
63
tests/ui/while_let_loop.stderr
Normal file
|
@ -0,0 +1,63 @@
|
|||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/while_let_loop.rs:5:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | if let Some(_x) = y {
|
||||
LL | | let _v = 1;
|
||||
LL | | } else {
|
||||
LL | | break;
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(_x) = y { .. }`
|
||||
|
|
||||
= note: `-D clippy::while-let-loop` implied by `-D warnings`
|
||||
|
||||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/while_let_loop.rs:22:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | match y {
|
||||
LL | | Some(_x) => true,
|
||||
LL | | None => break,
|
||||
LL | | };
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(_x) = y { .. }`
|
||||
|
||||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/while_let_loop.rs:29:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let x = match y {
|
||||
LL | | Some(x) => x,
|
||||
LL | | None => break,
|
||||
... |
|
||||
LL | | let _str = "foo";
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(x) = y { .. }`
|
||||
|
||||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/while_let_loop.rs:38:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let x = match y {
|
||||
LL | | Some(x) => x,
|
||||
LL | | None => break,
|
||||
... |
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(x) = y { .. }`
|
||||
|
||||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/while_let_loop.rs:68:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let (e, l) = match "".split_whitespace().next() {
|
||||
LL | | Some(word) => (word.is_empty(), word.len()),
|
||||
LL | | None => break,
|
||||
... |
|
||||
LL | | let _ = (e, l);
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(word) = "".split_whitespace().next() { .. }`
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
|
@ -1,73 +1,7 @@
|
|||
#![warn(clippy::while_let_loop, clippy::empty_loop, clippy::while_let_on_iterator)]
|
||||
#![allow(dead_code, clippy::never_loop, unused, clippy::cognitive_complexity)]
|
||||
#![warn(clippy::while_let_on_iterator)]
|
||||
#![allow(clippy::never_loop, clippy::cognitive_complexity)]
|
||||
|
||||
fn main() {
|
||||
let y = Some(true);
|
||||
loop {
|
||||
if let Some(_x) = y {
|
||||
let _v = 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
loop {
|
||||
// no error, break is not in else clause
|
||||
if let Some(_x) = y {
|
||||
let _v = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
loop {
|
||||
match y {
|
||||
Some(_x) => true,
|
||||
None => break,
|
||||
};
|
||||
}
|
||||
loop {
|
||||
let x = match y {
|
||||
Some(x) => x,
|
||||
None => break,
|
||||
};
|
||||
let _x = x;
|
||||
let _str = "foo";
|
||||
}
|
||||
loop {
|
||||
let x = match y {
|
||||
Some(x) => x,
|
||||
None => break,
|
||||
};
|
||||
{
|
||||
let _a = "bar";
|
||||
};
|
||||
{
|
||||
let _b = "foobar";
|
||||
}
|
||||
}
|
||||
loop {
|
||||
// no error, else branch does something other than break
|
||||
match y {
|
||||
Some(_x) => true,
|
||||
_ => {
|
||||
let _z = 1;
|
||||
break;
|
||||
},
|
||||
};
|
||||
}
|
||||
while let Some(x) = y {
|
||||
// no error, obviously
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// #675, this used to have a wrong suggestion
|
||||
loop {
|
||||
let (e, l) = match "".split_whitespace().next() {
|
||||
Some(word) => (word.is_empty(), word.len()),
|
||||
None => break,
|
||||
};
|
||||
|
||||
let _ = (e, l);
|
||||
}
|
||||
|
||||
let mut iter = 1..20;
|
||||
while let Option::Some(x) = iter.next() {
|
||||
println!("{}", x);
|
||||
|
@ -116,36 +50,6 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
// regression test (#360)
|
||||
// this should not panic
|
||||
// it's ok if further iterations of the lint
|
||||
// cause this function to trigger it
|
||||
fn no_panic<T>(slice: &[T]) {
|
||||
let mut iter = slice.iter();
|
||||
loop {
|
||||
let _ = match iter.next() {
|
||||
Some(ele) => ele,
|
||||
None => break,
|
||||
};
|
||||
loop {}
|
||||
}
|
||||
}
|
||||
|
||||
fn issue1017() {
|
||||
let r: Result<u32, u32> = Ok(42);
|
||||
let mut len = 1337;
|
||||
|
||||
loop {
|
||||
match r {
|
||||
Err(_) => len = 0,
|
||||
Ok(length) => {
|
||||
len = length;
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Issue #1188
|
||||
fn refutable() {
|
||||
let a = [42, 1337];
|
||||
|
@ -194,18 +98,6 @@ fn nested_loops() {
|
|||
}
|
||||
}
|
||||
|
||||
fn issue1948() {
|
||||
// should not trigger clippy::while_let_loop lint because break passes an expression
|
||||
let a = Some(10);
|
||||
let b = loop {
|
||||
if let Some(c) = a {
|
||||
break Some(c);
|
||||
} else {
|
||||
break None;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn issue1121() {
|
||||
use std::collections::HashSet;
|
||||
let mut values = HashSet::new();
|
||||
|
@ -238,18 +130,3 @@ fn issue3670() {
|
|||
let _ = elem.or_else(|| *iter.next()?);
|
||||
}
|
||||
}
|
||||
|
||||
fn issue771() {
|
||||
let mut a = 100;
|
||||
let b = Some(true);
|
||||
loop {
|
||||
if a > 10 {
|
||||
break;
|
||||
}
|
||||
|
||||
match b {
|
||||
Some(_) => a = 0,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
34
tests/ui/while_let_on_iterator.stderr
Normal file
34
tests/ui/while_let_on_iterator.stderr
Normal file
|
@ -0,0 +1,34 @@
|
|||
error: this loop could be written as a `for` loop
|
||||
--> $DIR/while_let_on_iterator.rs:6: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_let_on_iterator.rs:11: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_let_on_iterator.rs:16:25
|
||||
|
|
||||
LL | while let Some(_) = iter.next() {}
|
||||
| ^^^^^^^^^^^ help: try: `for _ in iter { .. }`
|
||||
|
||||
error: this loop could be written as a `for` loop
|
||||
--> $DIR/while_let_on_iterator.rs:95: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_let_on_iterator.rs:120:26
|
||||
|
|
||||
LL | while let Some(..) = values.iter().next() {
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter() { .. }`
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
|
@ -1,115 +0,0 @@
|
|||
error: this loop could be written as a `while let` loop
|
||||
--> $DIR/while_loop.rs:6:5
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | if let Some(_x) = y {
|
||||
LL | | let _v = 1;
|
||||
LL | | } else {
|
||||
LL | | break;
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_____^ help: try: `while let Some(_x) = y { .. }`
|
||||
|
|
||||
= 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
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | match y {
|
||||
LL | | Some(_x) => true,
|
||||
LL | | None => break,
|
||||
LL | | };
|
||||
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
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let x = match y {
|
||||
LL | | Some(x) => x,
|
||||
LL | | None => break,
|
||||
... |
|
||||
LL | | let _str = "foo";
|
||||
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
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let x = match y {
|
||||
LL | | Some(x) => x,
|
||||
LL | | None => break,
|
||||
... |
|
||||
LL | | }
|
||||
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
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let (e, l) = match "".split_whitespace().next() {
|
||||
LL | | Some(word) => (word.is_empty(), word.len()),
|
||||
LL | | None => break,
|
||||
... |
|
||||
LL | | let _ = (e, l);
|
||||
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
|
||||
|
|
||||
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
|
||||
|
|
||||
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
|
||||
|
|
||||
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
|
||||
|
|
||||
LL | / loop {
|
||||
LL | | let _ = match iter.next() {
|
||||
LL | | Some(ele) => ele,
|
||||
LL | | None => break,
|
||||
LL | | };
|
||||
LL | | loop {}
|
||||
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
|
||||
|
|
||||
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:191: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:228:26
|
||||
|
|
||||
LL | while let Some(..) = values.iter().next() {
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter() { .. }`
|
||||
|
||||
error: aborting due to 12 previous errors
|
||||
|
Loading…
Reference in a new issue