diff options
| author | Taiki Endo <te316e89@gmail.com> | 2019-03-12 03:40:30 +0900 |
|---|---|---|
| committer | Taiki Endo <te316e89@gmail.com> | 2019-03-12 03:40:30 +0900 |
| commit | 1bc7da2fec5192a4ab98ec5b348812a540a1d02f (patch) | |
| tree | 1bb9cb9dc6a3fcb3f3ef376a6a68a28918d30516 /tests | |
| parent | 1cdac4a9c798f8e22a4db220020713175eb9159b (diff) | |
| download | rust-1bc7da2fec5192a4ab98ec5b348812a540a1d02f.tar.gz rust-1bc7da2fec5192a4ab98ec5b348812a540a1d02f.zip | |
Fix `needless_continue` false positive
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/needless_continue.rs | 66 | ||||
| -rw-r--r-- | tests/ui/needless_continue.stderr | 48 |
2 files changed, 110 insertions, 4 deletions
diff --git a/tests/ui/needless_continue.rs b/tests/ui/needless_continue.rs index 8bb1ba6edb5..5da95647f2c 100644 --- a/tests/ui/needless_continue.rs +++ b/tests/ui/needless_continue.rs @@ -1,3 +1,5 @@ +#![warn(clippy::needless_continue)] + macro_rules! zero { ($x:expr) => { $x == 0 @@ -10,7 +12,6 @@ macro_rules! nonzero { }; } -#[warn(clippy::needless_continue)] fn main() { let mut i = 1; while i < 10 { @@ -49,3 +50,66 @@ fn main() { println!("bleh"); } } + +mod issue_2329 { + fn condition() -> bool { + unimplemented!() + } + fn update_condition() {} + + // only the outer loop has a label + fn foo() { + 'outer: loop { + println!("Entry"); + while condition() { + update_condition(); + if condition() { + println!("foo-1"); + } else { + continue 'outer; // should not lint here + } + println!("foo-2"); + + update_condition(); + if condition() { + continue 'outer; // should not lint here + } else { + println!("foo-3"); + } + println!("foo-4"); + } + } + } + + // both loops have labels + fn bar() { + 'outer: loop { + println!("Entry"); + 'inner: while condition() { + update_condition(); + if condition() { + println!("bar-1"); + } else { + continue 'outer; // should not lint here + } + println!("bar-2"); + + update_condition(); + if condition() { + println!("bar-3"); + } else { + continue 'inner; // should lint here + } + println!("bar-4"); + + update_condition(); + if condition() { + continue; // should lint here + } else { + println!("bar-5"); + } + println!("bar-6"); + } + } + } +} diff --git a/tests/ui/needless_continue.stderr b/tests/ui/needless_continue.stderr index 763eaf3a70e..340ae66dae4 100644 --- a/tests/ui/needless_continue.stderr +++ b/tests/ui/needless_continue.stderr @@ -1,6 +1,6 @@ error: This else block is redundant. - --> $DIR/needless_continue.rs:27:16 + --> $DIR/needless_continue.rs:28:16 | LL | } else { | ________________^ @@ -37,7 +37,7 @@ LL | | } error: There is no need for an explicit `else` block for this `if` expression - --> $DIR/needless_continue.rs:42:9 + --> $DIR/needless_continue.rs:43:9 | LL | / if (zero!(i % 2) || nonzero!(i % 5)) && i % 3 != 0 { LL | | continue; @@ -55,5 +55,47 @@ LL | | } println!("Jabber"); ... -error: aborting due to 2 previous errors +error: This else block is redundant. + + --> $DIR/needless_continue.rs:100:24 + | +LL | } else { + | ________________________^ +LL | | continue 'inner; // should lint here +LL | | } + | |_________________^ + | + = help: Consider dropping the else clause and merging the code that follows (in the loop) with the if block, like so: + if condition() { + println!("bar-3"); + // Merged code follows...println!("bar-4"); + update_condition(); + if condition() { + continue; // should lint here + } else { + println!("bar-5"); + } + println!("bar-6"); + } + + +error: There is no need for an explicit `else` block for this `if` expression + + --> $DIR/needless_continue.rs:106:17 + | +LL | / if condition() { +LL | | continue; // should lint here +LL | | } else { +LL | | println!("bar-5"); +LL | | } + | |_________________^ + | + = help: Consider dropping the else clause, and moving out the code in the else block, like so: + if condition() { + continue; + } + println!("bar-5"); + ... + +error: aborting due to 4 previous errors |
