Emit needless_continue warning if loop ends on continue

This commit is contained in:
F3real 2021-07-21 23:15:29 +02:00
parent 24ec35a904
commit 9d6127cdb0
3 changed files with 27 additions and 9 deletions

View file

@ -370,14 +370,14 @@ 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 loop_block.stmts.len() == 1;
if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.first().unwrap().kind;
if !loop_block.stmts.is_empty();
if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind;
if let ast::ExprKind::Continue(_) = statement.kind;
then {
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
loop_block.stmts.first().unwrap().span,
loop_block.stmts.last().unwrap().span,
MSG_REDUNDANT_CONTINUE_EXPRESSION,
None,
DROP_CONTINUE_EXPRESSION_MSG,

View file

@ -49,8 +49,18 @@ fn main() {
println!("bleh");
}
}
fn simple_loop() {
loop {
continue;
continue; // should lint here
}
}
fn simple_loop2() {
loop {
println!("bleh");
continue; // should lint here
}
}

View file

@ -55,15 +55,23 @@ LL | | }
}
error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:53:9
--> $DIR/needless_continue.rs:56:9
|
LL | continue;
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 `else` block is redundant
--> $DIR/needless_continue.rs:103:24
--> $DIR/needless_continue.rs:113:24
|
LL | } else {
| ________________________^
@ -86,7 +94,7 @@ LL | | }
}
error: there is no need for an explicit `else` block for this `if` expression
--> $DIR/needless_continue.rs:109:17
--> $DIR/needless_continue.rs:119:17
|
LL | / if condition() {
LL | | continue; // should lint here
@ -103,5 +111,5 @@ LL | | }
println!("bar-5");
}
error: aborting due to 5 previous errors
error: aborting due to 6 previous errors