Control statements are nested deeper than the configured maximum
What it finds
Each if, for, switch or select inside another adds a condition that the reader must keep in mind to understand the code within. Past a few levels the innermost code is hard to follow and hard to test; returning early, or moving the inner part into a function, flattens it.
The depth of a statement is the number of if, for, switch, type switch and select statements it is inside of, counting itself, within one function; an else if counts as part of its chain rather than as a level, and a function literal starts again from zero. A statement is reported when its depth first goes beyond the parameter max_depth, 5 by default, and statements nested in it are not reported again.
From the rule's source, deep_nesting.rs.
package p func f(xs [][]int, ok bool) int { n := 0 for _, row := range xs { for _, x := range row { if x > 0 { switch x { case 1: if ok { for i := 0; i < x; i++ {tx0 reports: this for is nested 6 levels deep, more than 5 if i > 2 { n++ } } } } } else if x < 0 { n-- } } } return n} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:deep-nesting" = "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:deep-nesting] reason="..."tx0 explain tx0/go:deep-nesting shows the same in the terminal. Found a false positive? Report it.