Skip to content

Commit

Permalink
Lint on continue expression without semi-colon
Browse files Browse the repository at this point in the history
  • Loading branch information
F3real committed Jul 22, 2021
1 parent 9d6127c commit c3452f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/needless_continue.rs
Expand Up @@ -371,7 +371,8 @@ fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) {
if_chain! {
if let ast::ExprKind::Loop(loop_block, ..) = &expr.kind;
if !loop_block.stmts.is_empty();
if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind;
if let ast::StmtKind::Expr(ref statement)
| ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind;
if let ast::ExprKind::Continue(_) = statement.kind;
then {
span_lint_and_help(
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/needless_continue.rs
Expand Up @@ -64,6 +64,21 @@ fn simple_loop2() {
}
}

#[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
22 changes: 19 additions & 3 deletions tests/ui/needless_continue.stderr
Expand Up @@ -70,8 +70,24 @@ 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:113:24
--> $DIR/needless_continue.rs:128:24
|
LL | } else {
| ________________________^
Expand All @@ -94,7 +110,7 @@ LL | | }
}

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

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

0 comments on commit c3452f3

Please sign in to comment.