Skip to content

Commit

Permalink
Auto merge of #7477 - F3real:needless_continue, r=flip1995
Browse files Browse the repository at this point in the history
Enhance needless continue to detect loop {continue;}

Fixes #7417

changelog: Report [`needless_continue`] in `loop { continue; }` case
  • Loading branch information
bors committed Jul 26, 2021
2 parents 6d9036b + 045dbb5 commit 02d70f3
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
20 changes: 20 additions & 0 deletions clippy_lints/src/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ struct LintData<'a> {
block_stmts: &'a [ast::Stmt],
}

const MSG_REDUNDANT_CONTINUE_EXPRESSION: &str = "this `continue` expression is redundant";

const MSG_REDUNDANT_ELSE_BLOCK: &str = "this `else` block is redundant";

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

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

const DROP_CONTINUE_EXPRESSION_MSG: &str = "consider dropping the `continue` expression";

fn emit_warning<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>, header: &str, typ: LintType) {
// snip is the whole *help* message that appears after the warning.
// message is the warning message.
Expand Down Expand Up @@ -364,6 +368,22 @@ 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 let Some(last_stmt) = loop_block.stmts.last();
if let ast::StmtKind::Expr(inner_expr) | ast::StmtKind::Semi(inner_expr) = &last_stmt.kind;
if let ast::ExprKind::Continue(_) = inner_expr.kind;
then {
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
last_stmt.span,
MSG_REDUNDANT_CONTINUE_EXPRESSION,
None,
DROP_CONTINUE_EXPRESSION_MSG,
);
}
}
with_loop_block(expr, |loop_block, label| {
for (i, stmt) in loop_block.stmts.iter().enumerate() {
with_if_expr(stmt, |if_expr, cond, then_block, else_expr| {
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ fn main() {
}
}

fn simple_loop() {
loop {
continue; // should lint here
}
}

fn simple_loop2() {
loop {
println!("bleh");
continue; // should lint here
}
}

#[rustfmt::skip]
fn simple_loop3() {
loop {
continue // should lint here
}
}

#[rustfmt::skip]
fn simple_loop4() {
loop {
println!("bleh");
continue // should lint here
}
}

mod issue_2329 {
fn condition() -> bool {
unimplemented!()
Expand Down
38 changes: 35 additions & 3 deletions tests/ui/needless_continue.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,40 @@ LL | | }
println!("Jabber");
}

error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:56:9
|
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 `continue` expression is redundant
--> $DIR/needless_continue.rs:70:9
|
LL | continue // should lint here
| ^^^^^^^^
|
= help: consider dropping the `continue` expression

error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:78:9
|
LL | continue // should lint here
| ^^^^^^^^
|
= help: consider dropping the `continue` expression

error: this `else` block is redundant
--> $DIR/needless_continue.rs:100:24
--> $DIR/needless_continue.rs:128:24
|
LL | } else {
| ________________________^
Expand All @@ -78,7 +110,7 @@ LL | | }
}

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

error: aborting due to 4 previous errors
error: aborting due to 8 previous errors

0 comments on commit 02d70f3

Please sign in to comment.