From c3452f3bd261e029b361c94ba8ed3bb909fde5fb Mon Sep 17 00:00:00 2001 From: F3real Date: Thu, 22 Jul 2021 22:21:34 +0200 Subject: [PATCH] Lint on continue expression without semi-colon --- clippy_lints/src/needless_continue.rs | 3 ++- tests/ui/needless_continue.rs | 15 +++++++++++++++ tests/ui/needless_continue.stderr | 22 +++++++++++++++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 0c2b382b3d2..a1f224e7d3a 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -371,7 +371,8 @@ fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) { if_chain! { if let ast::ExprKind::Loop(loop_block, ..) = &expr.kind; if !loop_block.stmts.is_empty(); - if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind; + if let ast::StmtKind::Expr(ref statement) + | ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind; if let ast::ExprKind::Continue(_) = statement.kind; then { span_lint_and_help( diff --git a/tests/ui/needless_continue.rs b/tests/ui/needless_continue.rs index 443a21f0268..83ee27f4887 100644 --- a/tests/ui/needless_continue.rs +++ b/tests/ui/needless_continue.rs @@ -64,6 +64,21 @@ fn simple_loop2() { } } +#[rustfmt::skip] +fn simple_loop3() { + loop { + continue // should lint here + } +} + +#[rustfmt::skip] +fn simple_loop4() { + loop { + println!("bleh"); + continue // should lint here + } +} + mod issue_2329 { fn condition() -> bool { unimplemented!() diff --git a/tests/ui/needless_continue.stderr b/tests/ui/needless_continue.stderr index 8042b740c73..22b86f25e8f 100644 --- a/tests/ui/needless_continue.stderr +++ b/tests/ui/needless_continue.stderr @@ -70,8 +70,24 @@ LL | continue; // should lint here | = help: consider dropping the `continue` expression +error: this `continue` expression is redundant + --> $DIR/needless_continue.rs:70:9 + | +LL | continue // should lint here + | ^^^^^^^^ + | + = help: consider dropping the `continue` expression + +error: this `continue` expression is redundant + --> $DIR/needless_continue.rs:78:9 + | +LL | continue // should lint here + | ^^^^^^^^ + | + = help: consider dropping the `continue` expression + error: this `else` block is redundant - --> $DIR/needless_continue.rs:113:24 + --> $DIR/needless_continue.rs:128:24 | LL | } else { | ________________________^ @@ -94,7 +110,7 @@ LL | | } } error: there is no need for an explicit `else` block for this `if` expression - --> $DIR/needless_continue.rs:119:17 + --> $DIR/needless_continue.rs:134:17 | LL | / if condition() { LL | | continue; // should lint here @@ -111,5 +127,5 @@ LL | | } println!("bar-5"); } -error: aborting due to 6 previous errors +error: aborting due to 8 previous errors -- 2.44.0