From 9d6127cdb0bead8f3a19440e9029edc9bf2e9a25 Mon Sep 17 00:00:00 2001 From: F3real Date: Wed, 21 Jul 2021 23:15:29 +0200 Subject: [PATCH] Emit needless_continue warning if loop ends on continue --- clippy_lints/src/needless_continue.rs | 6 +++--- tests/ui/needless_continue.rs | 12 +++++++++++- tests/ui/needless_continue.stderr | 18 +++++++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 6191747184b..0c2b382b3d2 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -370,14 +370,14 @@ fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data: 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.len() == 1; - if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.first().unwrap().kind; + if !loop_block.stmts.is_empty(); + if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind; if let ast::ExprKind::Continue(_) = statement.kind; then { span_lint_and_help( cx, NEEDLESS_CONTINUE, - loop_block.stmts.first().unwrap().span, + loop_block.stmts.last().unwrap().span, MSG_REDUNDANT_CONTINUE_EXPRESSION, None, DROP_CONTINUE_EXPRESSION_MSG, diff --git a/tests/ui/needless_continue.rs b/tests/ui/needless_continue.rs index 544b7e66a27..443a21f0268 100644 --- a/tests/ui/needless_continue.rs +++ b/tests/ui/needless_continue.rs @@ -49,8 +49,18 @@ fn main() { println!("bleh"); } +} + +fn simple_loop() { loop { - continue; + continue; // should lint here + } +} + +fn simple_loop2() { + loop { + println!("bleh"); + continue; // should lint here } } diff --git a/tests/ui/needless_continue.stderr b/tests/ui/needless_continue.stderr index 6e81acdc358..8042b740c73 100644 --- a/tests/ui/needless_continue.stderr +++ b/tests/ui/needless_continue.stderr @@ -55,15 +55,23 @@ LL | | } } error: this `continue` expression is redundant - --> $DIR/needless_continue.rs:53:9 + --> $DIR/needless_continue.rs:56:9 | -LL | continue; +LL | continue; // should lint here + | ^^^^^^^^^ + | + = help: consider dropping the `continue` expression + +error: this `continue` expression is redundant + --> $DIR/needless_continue.rs:63:9 + | +LL | continue; // should lint here | ^^^^^^^^^ | = help: consider dropping the `continue` expression error: this `else` block is redundant - --> $DIR/needless_continue.rs:103:24 + --> $DIR/needless_continue.rs:113:24 | LL | } else { | ________________________^ @@ -86,7 +94,7 @@ LL | | } } error: there is no need for an explicit `else` block for this `if` expression - --> $DIR/needless_continue.rs:109:17 + --> $DIR/needless_continue.rs:119:17 | LL | / if condition() { LL | | continue; // should lint here @@ -103,5 +111,5 @@ LL | | } println!("bar-5"); } -error: aborting due to 5 previous errors +error: aborting due to 6 previous errors -- 2.44.0