Skip to content

Commit b157411

Browse files
authored
Consecutive returns dont decrease cognitive Complexity level anymore (#14460)
changelog: [`cognitive_complexity`]: Consecutive return calls decreased complexity level of the function by 1. fixes #14422
2 parents 8dfcaa0 + c7fbcf4 commit b157411

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

clippy_lints/src/cognitive_complexity.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl CognitiveComplexity {
6262

6363
let mut cc = 1u64;
6464
let mut returns = 0u64;
65+
let mut prev_expr: Option<&ExprKind<'tcx>> = None;
6566
let _: Option<!> = for_each_expr_without_closures(expr, |e| {
6667
match e.kind {
6768
ExprKind::If(_, _, _) => {
@@ -73,9 +74,14 @@ impl CognitiveComplexity {
7374
}
7475
cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
7576
},
76-
ExprKind::Ret(_) => returns += 1,
77+
ExprKind::Ret(_) => {
78+
if !matches!(prev_expr, Some(ExprKind::Ret(_))) {
79+
returns += 1;
80+
}
81+
},
7782
_ => {},
7883
}
84+
prev_expr = Some(&e.kind);
7985
ControlFlow::Continue(())
8086
});
8187

tests/ui/cognitive_complexity.rs

+17
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,20 @@ mod issue9300 {
448448
}
449449
}
450450
}
451+
452+
#[clippy::cognitive_complexity = "2"]
453+
mod issue14422 {
454+
fn foo() {
455+
for _ in 0..10 {
456+
println!("hello there");
457+
}
458+
}
459+
460+
fn bar() {
461+
for _ in 0..10 {
462+
println!("hello there");
463+
}
464+
return;
465+
return;
466+
}
467+
}

0 commit comments

Comments
 (0)