diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 619174718..0c2b382b3 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 544b7e66a..443a21f02 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 6e81acdc3..8042b740c 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