about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTimo <30553356+y21@users.noreply.github.com>2025-04-12 16:53:50 +0000
committerGitHub <noreply@github.com>2025-04-12 16:53:50 +0000
commitb157411dba22a44f5664ad3ec33e6a5ae47d9e44 (patch)
tree93760c27881739baf9d4bdf6895207e2c1230809
parent8dfcaa0590a29ba8d5ddea87cd57d61e5c9224f7 (diff)
parentc7fbcf4c339aa2162ed42d7beea1e927c2cd6c0e (diff)
downloadrust-b157411dba22a44f5664ad3ec33e6a5ae47d9e44.tar.gz
rust-b157411dba22a44f5664ad3ec33e6a5ae47d9e44.zip
Consecutive returns dont decrease cognitive Complexity level anymore (#14460)
changelog: [`cognitive_complexity`]: Consecutive return calls decreased
complexity level of the function by 1.

fixes rust-lang/rust-clippy#14422
-rw-r--r--clippy_lints/src/cognitive_complexity.rs8
-rw-r--r--tests/ui/cognitive_complexity.rs17
2 files changed, 24 insertions, 1 deletions
diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs
index a1ff20dee72..1d44c7e9c88 100644
--- a/clippy_lints/src/cognitive_complexity.rs
+++ b/clippy_lints/src/cognitive_complexity.rs
@@ -62,6 +62,7 @@ impl CognitiveComplexity {
 
         let mut cc = 1u64;
         let mut returns = 0u64;
+        let mut prev_expr: Option<&ExprKind<'tcx>> = None;
         let _: Option<!> = for_each_expr_without_closures(expr, |e| {
             match e.kind {
                 ExprKind::If(_, _, _) => {
@@ -73,9 +74,14 @@ impl CognitiveComplexity {
                     }
                     cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
                 },
-                ExprKind::Ret(_) => returns += 1,
+                ExprKind::Ret(_) => {
+                    if !matches!(prev_expr, Some(ExprKind::Ret(_))) {
+                        returns += 1;
+                    }
+                },
                 _ => {},
             }
+            prev_expr = Some(&e.kind);
             ControlFlow::Continue(())
         });
 
diff --git a/tests/ui/cognitive_complexity.rs b/tests/ui/cognitive_complexity.rs
index 2dbec955f63..88033a7b674 100644
--- a/tests/ui/cognitive_complexity.rs
+++ b/tests/ui/cognitive_complexity.rs
@@ -448,3 +448,20 @@ mod issue9300 {
         }
     }
 }
+
+#[clippy::cognitive_complexity = "2"]
+mod issue14422 {
+    fn foo() {
+        for _ in 0..10 {
+            println!("hello there");
+        }
+    }
+
+    fn bar() {
+        for _ in 0..10 {
+            println!("hello there");
+        }
+        return;
+        return;
+    }
+}