Open epic-64 opened 1 year ago
I found the following test (comments added by me). https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/cognitive_complexity.rs#L91 https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/cognitive_complexity.stderr#L10
fn kaboom() {
let n = 0;
'a: for i in 0..20 { // +1 (nesting level 0)
'b: for j in i..20 { // +2 (nesting level 1)
for k in j..20 { // +3 (nesting level 2)
if k == 5 { // +4 (nesting level 3)
break 'b;
}
if j == 3 && k == 6 { // +5 (nesting level 3, +1 for introducting a boolean operation)
continue 'a;
}
if k == j { // +4 (nesting level 3)
continue;
}
println!("bake");
}
}
println!("cake");
}
// Cognitive Complexity: 19
}
By counting I arrive at a value of 19, but the test suite asserts (and measures) a value of 7. Clearly there is some mismatch of definitions here. I go by the original specification https://www.sonarsource.com/docs/CognitiveComplexity.pdf
Summary
I tried the cognitive_complexity lint on a basic code example. The result does not match up.
Input:
Expected result: 21 Actual Result: 7
Additional Details To my understanding, the cognitive complexty of the main function is 21, as each for loop adds a value of 1, plus 1 for each level of nesting that it is already in.
However the warning I get says that it is 7:
Reproducer
I tried this code:
Given this source code:
I expected to see this happen: warning: the function has a cognitive complexity of (21/1)
Instead, this happened: warning: the function has a cognitive complexity of (7/1)
Version
Additional Labels
No response