A function's branches, weighted by how deeply they are nested, exceed the configured maximum
What it finds
Counting paths treats a flat list of cases like a deep tangle of loops in conditions, although the second is much harder to follow. This measure weighs each branch by how deeply it is nested, so it grows with the effort of reading the function rather than with the number of its tests.
The measure is defined for tx0 as follows. Each if, else if, else, switch, type switch, select and for adds one; if, switch, select and for add one more for every one of those statements and function literals they are nested in. A goto, and a break or continue with a label, add one. In a condition, each run of the same boolean operator adds one, so a && b && c adds one and a && b || c two. Function literals count toward the function they are written in. The maximum is the parameter max_complexity, 15 by default.
From the rule's source, nesting_weighted_complexity.rs.
package p func tangled(xs [][]int, ok bool) int {tx0 reports: tangled has a nesting-weighted complexity of 16, more than 15 total := 0 for _, row := range xs { for _, x := range row { if x > 0 && ok || x < -5 { if x%2 == 0 || x > 10 && ok { total += x } else if x < 0 { total -= x } else { total++ } } } } return total} func flat(n int) int { switch n { case 1: return 1 case 2: return 2 } if n > 3 { return 3 } return 0} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:nesting-weighted-complexity" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:nesting-weighted-complexity] reason="..."tx0 explain tx0/go:nesting-weighted-complexity shows the same in the terminal. Found a false positive? Report it.