Enhance needless continue to detect loop {continue;}
This commit is contained in:
parent
610381455c
commit
24ec35a904
@ -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` \
|
||||
@ -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.
|
||||
@ -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 loop_block.stmts.len() == 1;
|
||||
if let ast::StmtKind::Semi(ref statement) = loop_block.stmts.first().unwrap().kind;
|
||||
if let ast::ExprKind::Continue(_) = statement.kind;
|
||||
then {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
NEEDLESS_CONTINUE,
|
||||
loop_block.stmts.first().unwrap().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| {
|
||||
|
@ -49,6 +49,9 @@ fn main() {
|
||||
|
||||
println!("bleh");
|
||||
}
|
||||
loop {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
mod issue_2329 {
|
||||
|
@ -54,8 +54,16 @@ LL | | }
|
||||
println!("Jabber");
|
||||
}
|
||||
|
||||
error: this `continue` expression is redundant
|
||||
--> $DIR/needless_continue.rs:53:9
|
||||
|
|
||||
LL | continue;
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= help: consider dropping the `continue` expression
|
||||
|
||||
error: this `else` block is redundant
|
||||
--> $DIR/needless_continue.rs:100:24
|
||||
--> $DIR/needless_continue.rs:103:24
|
||||
|
|
||||
LL | } else {
|
||||
| ________________________^
|
||||
@ -78,7 +86,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:109:17
|
||||
|
|
||||
LL | / if condition() {
|
||||
LL | | continue; // should lint here
|
||||
@ -95,5 +103,5 @@ LL | | }
|
||||
println!("bar-5");
|
||||
}
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user