Skip to content

Commit

Permalink
Auto merge of #4069 - mikerite:while_loop_test_split, r=phansch
Browse files Browse the repository at this point in the history
Reorganize "while loop" tests

cc #2038

changelog: none
  • Loading branch information
bors committed May 9, 2019
2 parents 341c96a + dcfe380 commit d056ea6
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 240 deletions.
12 changes: 12 additions & 0 deletions tests/ui/ice-360.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions tests/ui/ice-360.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 119 additions & 0 deletions tests/ui/while_let_loop.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 0 deletions tests/ui/while_let_loop.stderr
Original file line number Diff line number Diff line change
@@ -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

127 changes: 2 additions & 125 deletions tests/ui/while_loop.rs → tests/ui/while_let_on_iterator.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
}
}
}
Loading

0 comments on commit d056ea6

Please sign in to comment.