]> git.lizzy.rs Git - rust.git/commitdiff
Emit needless_continue warning if loop ends on continue
authorF3real <stefan92ff@yandex.com>
Wed, 21 Jul 2021 21:15:29 +0000 (23:15 +0200)
committerF3real <stefan92ff@yandex.com>
Wed, 21 Jul 2021 21:32:16 +0000 (23:32 +0200)
clippy_lints/src/needless_continue.rs
tests/ui/needless_continue.rs
tests/ui/needless_continue.stderr

index 6191747184b5ac8f2b7dd028a2fb1813b4393718..0c2b382b3d28ebd50daa378ebbff66d7a355024f 100644 (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;
 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,
         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,
                 MSG_REDUNDANT_CONTINUE_EXPRESSION,
                 None,
                 DROP_CONTINUE_EXPRESSION_MSG,
index 544b7e66a270a73e71adfc15505e6ff11925b0a3..443a21f0268e7c3fbac5edc8a66c2fd0efa40584 100644 (file)
@@ -49,8 +49,18 @@ fn main() {
 
         println!("bleh");
     }
 
         println!("bleh");
     }
+}
+
+fn simple_loop() {
     loop {
     loop {
-        continue;
+        continue; // should lint here
+    }
+}
+
+fn simple_loop2() {
+    loop {
+        println!("bleh");
+        continue; // should lint here
     }
 }
 
     }
 }
 
index 6e81acdc358425cf7cc77be5438f34fef7fde4eb..8042b740c73255c6e382ab6130b4e7023b1bb8e4 100644 (file)
@@ -55,15 +55,23 @@ LL | |         }
                    }
 
 error: this `continue` expression is redundant
                    }
 
 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
    |         ^^^^^^^^^
    |
    = 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 {
    |  ________________________^
    |
 LL |                   } else {
    |  ________________________^
@@ -86,7 +94,7 @@ LL | |                 }
                            }
 
 error: there is no need for an explicit `else` block for this `if` expression
                            }
 
 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
    |
 LL | /                 if condition() {
 LL | |                     continue; // should lint here
@@ -103,5 +111,5 @@ LL | |                 }
                                println!("bar-5");
                            }
 
                                println!("bar-5");
                            }
 
-error: aborting due to 5 previous errors
+error: aborting due to 6 previous errors