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

Commit 24ec35a

Browse files
committed
Enhance needless continue to detect loop {continue;}
1 parent 6103814 commit 24ec35a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clippy_lints/src/needless_continue.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ struct LintData<'a> {
273273
block_stmts: &'a [ast::Stmt],
274274
}
275275

276+
const MSG_REDUNDANT_CONTINUE_EXPRESSION: &str = "this `continue` expression is redundant";
277+
276278
const MSG_REDUNDANT_ELSE_BLOCK: &str = "this `else` block is redundant";
277279

278280
const MSG_ELSE_BLOCK_NOT_NEEDED: &str = "there is no need for an explicit `else` block for this `if` \
@@ -283,6 +285,8 @@ const DROP_ELSE_BLOCK_AND_MERGE_MSG: &str = "consider dropping the `else` clause
283285

284286
const DROP_ELSE_BLOCK_MSG: &str = "consider dropping the `else` clause";
285287

288+
const DROP_CONTINUE_EXPRESSION_MSG: &str = "consider dropping the `continue` expression";
289+
286290
fn emit_warning<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>, header: &str, typ: LintType) {
287291
// snip is the whole *help* message that appears after the warning.
288292
// message is the warning message.
@@ -364,6 +368,22 @@ fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data:
364368
}
365369

366370
fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) {
371+
if_chain! {
372+
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;
375+
if let ast::ExprKind::Continue(_) = statement.kind;
376+
then {
377+
span_lint_and_help(
378+
cx,
379+
NEEDLESS_CONTINUE,
380+
loop_block.stmts.first().unwrap().span,
381+
MSG_REDUNDANT_CONTINUE_EXPRESSION,
382+
None,
383+
DROP_CONTINUE_EXPRESSION_MSG,
384+
);
385+
}
386+
}
367387
with_loop_block(expr, |loop_block, label| {
368388
for (i, stmt) in loop_block.stmts.iter().enumerate() {
369389
with_if_expr(stmt, |if_expr, cond, then_block, else_expr| {

tests/ui/needless_continue.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ fn main() {
4949

5050
println!("bleh");
5151
}
52+
loop {
53+
continue;
54+
}
5255
}
5356

5457
mod issue_2329 {

tests/ui/needless_continue.stderr

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,16 @@ LL | | }
5454
println!("Jabber");
5555
}
5656

57+
error: this `continue` expression is redundant
58+
--> $DIR/needless_continue.rs:53:9
59+
|
60+
LL | continue;
61+
| ^^^^^^^^^
62+
|
63+
= help: consider dropping the `continue` expression
64+
5765
error: this `else` block is redundant
58-
--> $DIR/needless_continue.rs:100:24
66+
--> $DIR/needless_continue.rs:103:24
5967
|
6068
LL | } else {
6169
| ________________________^
@@ -78,7 +86,7 @@ LL | | }
7886
}
7987

8088
error: there is no need for an explicit `else` block for this `if` expression
81-
--> $DIR/needless_continue.rs:106:17
89+
--> $DIR/needless_continue.rs:109:17
8290
|
8391
LL | / if condition() {
8492
LL | | continue; // should lint here
@@ -95,5 +103,5 @@ LL | | }
95103
println!("bar-5");
96104
}
97105

98-
error: aborting due to 4 previous errors
106+
error: aborting due to 5 previous errors
99107

0 commit comments

Comments
 (0)