An else follows a branch that always returns, so its body could follow the if unindented
What it finds
if err != nil { return err } else { use(v) }: the first branch never falls through, so the else only indents the normal path. Go code keeps the normal path at the left margin and handles the exceptions in early returns.
The branch must end in a return, and the if must have no init statement, whose variables the else could be using, and must not itself be the else of another if. Chains with else if are left alone.
From the rule's source, else_after_return.rs.
package p func f(x int) int { if x < 0 { return -1 } else {tx0 reports: the if branch returns, so this else only indents the code in it x++ } if y := x * 2; y > 10 { return y } else { x += y } if x > 3 { x-- } else { x++ } return x} 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:else-after-return" = "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:else-after-return] reason="..."tx0 explain tx0/go:else-after-return shows the same in the terminal. Found a false positive? Report it.