From da97004e7217f88f40c39998e8406ceb383a9243 Mon Sep 17 00:00:00 2001 From: cerdelen Date: Mon, 24 Mar 2025 16:58:04 +0100 Subject: [PATCH 1/2] Multiple return statements doesnt decrease cognitive complexity level multiple times --- clippy_lints/src/cognitive_complexity.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/cognitive_complexity.rs b/clippy_lints/src/cognitive_complexity.rs index a1ff20dee721..1d44c7e9c88b 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(()) }); From c7fbcf4c339aa2162ed42d7beea1e927c2cd6c0e Mon Sep 17 00:00:00 2001 From: cerdelen Date: Fri, 11 Apr 2025 09:30:28 +0200 Subject: [PATCH 2/2] Test for Multiple return statements doesnt decrease cognitive complexity --- tests/ui/cognitive_complexity.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/ui/cognitive_complexity.rs b/tests/ui/cognitive_complexity.rs index 2dbec955f63f..88033a7b6741 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; + } +}