1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00
Commit Graph

3 Commits

Author SHA1 Message Date
AxiomaticFixedChimpanzee
b77bb1a9db fix: cognitive complexity nesting with if-else chains (#1268)
* test: prove bug in cognitive complexity rule

* fix: cognitive complexity nesting with if-else chains

Currently, an if-else chain will increase the nesting level and add the
nesting increment for every addition `else if` statement in an if-else
chain. This is incorrect behaviour; an `else if` statement should
increment complexity by 1 (regardless of current nesting level) and
leave the nesting level as-is.

For example, the following should yield a total complexity of 5:
```
for { // +1
    if a { // +2 (nesting = 1)
        foo()
    } else if b { // +1
        bar()
    } else if c { // +1
        baz()
    }
}
```

but the current implementation incorrectly increments the nesting level
with each `else if` and adds the nesting increment where it shouldn't:
```
for { // +1
    if a { // +2 (nesting = 1)
        foo()
    } else if b { // +3 (nesting = 2)
        bar()
    } else if c { // +4 (nesting = 3)
        baz()
    }
}
```
2025-03-13 08:31:26 +01:00
Oleksandr Redko
bb9e681da6 cognitive-complexity: handle direct recursion (#1149) 2024-12-01 12:16:06 +01:00
Oleksandr Redko
be95bfa705 refactor: rename files to follow Go convention 2024-11-11 19:31:18 +01:00