Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9d6127c

Browse files
committed
Emit needless_continue warning if loop ends on continue
1 parent 24ec35a commit 9d6127c

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

clippy_lints/src/needless_continue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data:
370370
fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) {
371371
if_chain! {
372372
if let ast::ExprKind::Loop(loop_block, ..) = &expr.kind;
373-
if loop_block.stmts.len() == 1;
374-
if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.first().unwrap().kind;
373+
if !loop_block.stmts.is_empty();
374+
if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind;
375375
if let ast::ExprKind::Continue(_) = statement.kind;
376376
then {
377377
span_lint_and_help(
378378
cx,
379379
NEEDLESS_CONTINUE,
380-
loop_block.stmts.first().unwrap().span,
380+
loop_block.stmts.last().unwrap().span,
381381
MSG_REDUNDANT_CONTINUE_EXPRESSION,
382382
None,
383383
DROP_CONTINUE_EXPRESSION_MSG,

tests/ui/needless_continue.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@ fn main() {
4949

5050
println!("bleh");
5151
}
52+
}
53+
54+
fn simple_loop() {
5255
loop {
53-
continue;
56+
continue; // should lint here
57+
}
58+
}
59+
60+
fn simple_loop2() {
61+
loop {
62+
println!("bleh");
63+
continue; // should lint here
5464
}
5565
}
5666

tests/ui/needless_continue.stderr

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,23 @@ LL | | }
5555
}
5656

5757
error: this `continue` expression is redundant
58-
--> $DIR/needless_continue.rs:53:9
58+
--> $DIR/needless_continue.rs:56:9
5959
|
60-
LL | continue;
60+
LL | continue; // should lint here
61+
| ^^^^^^^^^
62+
|
63+
= help: consider dropping the `continue` expression
64+
65+
error: this `continue` expression is redundant
66+
--> $DIR/needless_continue.rs:63:9
67+
|
68+
LL | continue; // should lint here
6169
| ^^^^^^^^^
6270
|
6371
= help: consider dropping the `continue` expression
6472

6573
error: this `else` block is redundant
66-
--> $DIR/needless_continue.rs:103:24
74+
--> $DIR/needless_continue.rs:113:24
6775
|
6876
LL | } else {
6977
| ________________________^
@@ -86,7 +94,7 @@ LL | | }
8694
}
8795

8896
error: there is no need for an explicit `else` block for this `if` expression
89-
--> $DIR/needless_continue.rs:109:17
97+
--> $DIR/needless_continue.rs:119:17
9098
|
9199
LL | / if condition() {
92100
LL | | continue; // should lint here
@@ -103,5 +111,5 @@ LL | | }
103111
println!("bar-5");
104112
}
105113

106-
error: aborting due to 5 previous errors
114+
error: aborting due to 6 previous errors
107115

0 commit comments

Comments
 (0)