Skip to content

Commit 02d70f3

Browse files
committed
Auto merge of #7477 - F3real:needless_continue, r=flip1995
Enhance needless continue to detect loop {continue;} Fixes #7417 changelog: Report [`needless_continue`] in `loop { continue; }` case
2 parents 6d9036b + 045dbb5 commit 02d70f3

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

clippy_lints/src/needless_continue.rs

+20
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 let Some(last_stmt) = loop_block.stmts.last();
374+
if let ast::StmtKind::Expr(inner_expr) | ast::StmtKind::Semi(inner_expr) = &last_stmt.kind;
375+
if let ast::ExprKind::Continue(_) = inner_expr.kind;
376+
then {
377+
span_lint_and_help(
378+
cx,
379+
NEEDLESS_CONTINUE,
380+
last_stmt.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

+28
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ fn main() {
5151
}
5252
}
5353

54+
fn simple_loop() {
55+
loop {
56+
continue; // should lint here
57+
}
58+
}
59+
60+
fn simple_loop2() {
61+
loop {
62+
println!("bleh");
63+
continue; // should lint here
64+
}
65+
}
66+
67+
#[rustfmt::skip]
68+
fn simple_loop3() {
69+
loop {
70+
continue // should lint here
71+
}
72+
}
73+
74+
#[rustfmt::skip]
75+
fn simple_loop4() {
76+
loop {
77+
println!("bleh");
78+
continue // should lint here
79+
}
80+
}
81+
5482
mod issue_2329 {
5583
fn condition() -> bool {
5684
unimplemented!()

tests/ui/needless_continue.stderr

+35-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,40 @@ LL | | }
5454
println!("Jabber");
5555
}
5656

57+
error: this `continue` expression is redundant
58+
--> $DIR/needless_continue.rs:56:9
59+
|
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
69+
| ^^^^^^^^^
70+
|
71+
= help: consider dropping the `continue` expression
72+
73+
error: this `continue` expression is redundant
74+
--> $DIR/needless_continue.rs:70:9
75+
|
76+
LL | continue // should lint here
77+
| ^^^^^^^^
78+
|
79+
= help: consider dropping the `continue` expression
80+
81+
error: this `continue` expression is redundant
82+
--> $DIR/needless_continue.rs:78:9
83+
|
84+
LL | continue // should lint here
85+
| ^^^^^^^^
86+
|
87+
= help: consider dropping the `continue` expression
88+
5789
error: this `else` block is redundant
58-
--> $DIR/needless_continue.rs:100:24
90+
--> $DIR/needless_continue.rs:128:24
5991
|
6092
LL | } else {
6193
| ________________________^
@@ -78,7 +110,7 @@ LL | | }
78110
}
79111

80112
error: there is no need for an explicit `else` block for this `if` expression
81-
--> $DIR/needless_continue.rs:106:17
113+
--> $DIR/needless_continue.rs:134:17
82114
|
83115
LL | / if condition() {
84116
LL | | continue; // should lint here
@@ -95,5 +127,5 @@ LL | | }
95127
println!("bar-5");
96128
}
97129

98-
error: aborting due to 4 previous errors
130+
error: aborting due to 8 previous errors
99131

0 commit comments

Comments
 (0)