about summary refs log tree commit diff
path: root/clippy_lints/src/needless_continue.rs
diff options
context:
space:
mode:
authorF3real <stefan92ff@yandex.com>2021-07-20 17:29:03 +0200
committerF3real <stefan92ff@yandex.com>2021-07-20 19:26:45 +0200
commit24ec35a90433b40fcca4272dce27c107f7be51d9 (patch)
tree2e3e8c28c97cca1234a32ecec09c12a63050be90 /clippy_lints/src/needless_continue.rs
parent610381455cb78a31695b94bc45ee580cf0cd7b38 (diff)
downloadrust-24ec35a90433b40fcca4272dce27c107f7be51d9.tar.gz
rust-24ec35a90433b40fcca4272dce27c107f7be51d9.zip
Enhance needless continue to detect loop {continue;}
Diffstat (limited to 'clippy_lints/src/needless_continue.rs')
-rw-r--r--clippy_lints/src/needless_continue.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs
index 91c97ef7c2a..6191747184b 100644
--- a/clippy_lints/src/needless_continue.rs
+++ b/clippy_lints/src/needless_continue.rs
@@ -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| {