You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
warning: the function has a cognitive complexity of (7/1)
--> src\main.rs:17:4
|
17 | fn main() -> Result<()> {
| ^^^^
|
note: the lint level is defined here
--> src\main.rs:1:9
|
1 | #![warn(clippy::cognitive_complexity)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: you could split it up into multiple smaller functions
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
fnkaboom(){let n = 0;'a:for i in0..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
@epic-64 yeah, sorry about that. I was the last to touch that lint.
In a nutshell: the lint is broken and the entire concept of linting "Cognitive Complexity" is nonsense. Don't believe what SonarSource says about its marvels; it's just not true, fam. That's how they got me.
It's a lint that cannot be accomplished with modern techniques. It's a good few years away, perhaps a decade or more.
#3793 Here you can see the logs of my investigation into it.
@flip1995 is there a way to archive this lint further down than Nursery? Maybe even... deleting it? There's at least a handful of users who've tried it and I don't think it would be honest to implement SonarSource's marketing whitepaper and call it a day.
Uh oh!
There was an error while loading. Please reload this page.
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
The text was updated successfully, but these errors were encountered: