-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4069 - mikerite:while_loop_test_split, r=phansch
Reorganize "while loop" tests cc #2038 changelog: none
- Loading branch information
Showing
7 changed files
with
254 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.