From 1cb94a7e6cd781eac576c43a3d91ff2b0628864b Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Wed, 8 May 2019 09:19:08 +0200 Subject: [PATCH 1/4] Split while_loop.rs tests Split out while_let_loop tests into separate file. Split ice-360 out into separate file. --- tests/ui/ice-360.rs | 12 ++++ tests/ui/ice-360.stderr | 24 +++++++ tests/ui/while_let_loop.rs | 119 +++++++++++++++++++++++++++++++ tests/ui/while_let_loop.stderr | 63 +++++++++++++++++ tests/ui/while_loop.rs | 123 --------------------------------- tests/ui/while_loop.stderr | 93 ++----------------------- 6 files changed, 224 insertions(+), 210 deletions(-) create mode 100644 tests/ui/ice-360.rs create mode 100644 tests/ui/ice-360.stderr create mode 100644 tests/ui/while_let_loop.rs create mode 100644 tests/ui/while_let_loop.stderr 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_loop.rs index a8506553e..ecc3fa371 100644 --- a/tests/ui/while_loop.rs +++ b/tests/ui/while_loop.rs @@ -2,72 +2,6 @@ #![allow(dead_code, clippy::never_loop, unused, 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_loop.stderr b/tests/ui/while_loop.stderr index 4c27ea0b9..36ada4e54 100644 --- a/tests/ui/while_loop.stderr +++ b/tests/ui/while_loop.stderr @@ -1,66 +1,5 @@ -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 + --> $DIR/while_loop.rs:6:33 | LL | while let Option::Some(x) = iter.next() { | ^^^^^^^^^^^ help: try: `for x in iter { .. }` @@ -68,48 +7,28 @@ LL | while let Option::Some(x) = iter.next() { = 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 + --> $DIR/while_loop.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_loop.rs:82:25 + --> $DIR/while_loop.rs:16: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 + --> $DIR/while_loop.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_loop.rs:228:26 + --> $DIR/while_loop.rs:120:26 | LL | while let Some(..) = values.iter().next() { | ^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter() { .. }` -error: aborting due to 12 previous errors +error: aborting due to 5 previous errors From 5149ed40a9d8b23e36f2e6c3caa1ca00be0c8491 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Wed, 8 May 2019 09:29:27 +0200 Subject: [PATCH 2/4] Rename while_loop tests to while_let_on_iterator --- tests/ui/{while_loop.rs => while_let_on_iterator.rs} | 0 tests/ui/{while_loop.stderr => while_let_on_iterator.stderr} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/{while_loop.rs => while_let_on_iterator.rs} (100%) rename tests/ui/{while_loop.stderr => while_let_on_iterator.stderr} (100%) diff --git a/tests/ui/while_loop.rs b/tests/ui/while_let_on_iterator.rs similarity index 100% rename from tests/ui/while_loop.rs rename to tests/ui/while_let_on_iterator.rs diff --git a/tests/ui/while_loop.stderr b/tests/ui/while_let_on_iterator.stderr similarity index 100% rename from tests/ui/while_loop.stderr rename to tests/ui/while_let_on_iterator.stderr From 225f86f2c30280910c44cc763c9e1a4bf485ea2a Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Thu, 9 May 2019 05:54:05 +0200 Subject: [PATCH 3/4] Fix while_let_on_iterator.stderr --- tests/ui/while_let_on_iterator.stderr | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ui/while_let_on_iterator.stderr b/tests/ui/while_let_on_iterator.stderr index 36ada4e54..03d2ef550 100644 --- a/tests/ui/while_let_on_iterator.stderr +++ b/tests/ui/while_let_on_iterator.stderr @@ -1,5 +1,5 @@ error: this loop could be written as a `for` loop - --> $DIR/while_loop.rs:6:33 + --> $DIR/while_let_on_iterator.rs:6:33 | LL | while let Option::Some(x) = iter.next() { | ^^^^^^^^^^^ help: try: `for x in iter { .. }` @@ -7,25 +7,25 @@ LL | while let Option::Some(x) = iter.next() { = 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:11:25 + --> $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_loop.rs:16:25 + --> $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_loop.rs:95:29 + --> $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_loop.rs:120:26 + --> $DIR/while_let_on_iterator.rs:120:26 | LL | while let Some(..) = values.iter().next() { | ^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter() { .. }` From dcfe380de4821b2122c64a5d25553eb6ea714a20 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Thu, 9 May 2019 05:55:24 +0200 Subject: [PATCH 4/4] Remove unused attrs in while_let_on_iterator tests --- tests/ui/while_let_on_iterator.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ui/while_let_on_iterator.rs b/tests/ui/while_let_on_iterator.rs index ecc3fa371..01838ee20 100644 --- a/tests/ui/while_let_on_iterator.rs +++ b/tests/ui/while_let_on_iterator.rs @@ -1,5 +1,5 @@ -#![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 mut iter = 1..20;