diff --git a/tests/ui/ice-360.rs b/tests/ui/ice-360.rs new file mode 100644 index 000000000..6555c19ca --- /dev/null +++ b/tests/ui/ice-360.rs @@ -0,0 +1,12 @@ +fn main() {} + +fn no_panic(slice: &[T]) { + let mut iter = slice.iter(); + loop { + let _ = match iter.next() { + Some(ele) => ele, + None => break, + }; + loop {} + } +} diff --git a/tests/ui/ice-360.stderr b/tests/ui/ice-360.stderr new file mode 100644 index 000000000..84e31eaf2 --- /dev/null +++ b/tests/ui/ice-360.stderr @@ -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 + diff --git a/tests/ui/while_let_loop.rs b/tests/ui/while_let_loop.rs new file mode 100644 index 000000000..3ce699f55 --- /dev/null +++ b/tests/ui/while_let_loop.rs @@ -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 = 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; + } + }; +} diff --git a/tests/ui/while_let_loop.stderr b/tests/ui/while_let_loop.stderr new file mode 100644 index 000000000..13dd0ee22 --- /dev/null +++ b/tests/ui/while_let_loop.stderr @@ -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 + diff --git a/tests/ui/while_loop.rs b/tests/ui/while_let_on_iterator.rs similarity index 51% rename from tests/ui/while_loop.rs rename to tests/ui/while_let_on_iterator.rs index a8506553e..01838ee20 100644 --- a/tests/ui/while_loop.rs +++ b/tests/ui/while_let_on_iterator.rs @@ -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(slice: &[T]) { - let mut iter = slice.iter(); - loop { - let _ = match iter.next() { - Some(ele) => ele, - None => break, - }; - loop {} - } -} - -fn issue1017() { - let r: Result = 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, - } - } -} diff --git a/tests/ui/while_let_on_iterator.stderr b/tests/ui/while_let_on_iterator.stderr new file mode 100644 index 000000000..03d2ef550 --- /dev/null +++ b/tests/ui/while_let_on_iterator.stderr @@ -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 + diff --git a/tests/ui/while_loop.stderr b/tests/ui/while_loop.stderr deleted file mode 100644 index 4c27ea0b9..000000000 --- a/tests/ui/while_loop.stderr +++ /dev/null @@ -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 -