Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance needless continue to detect loop {continue;} #7477

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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