An if-else-if chain has no final else for the case where no condition holds

tx0/go:else-if-without-elseallwarningmaintainability

What it finds

if a { ... } else if b { ... } with no final else: when a chain tests several conditions, the case where none holds is easy to forget, and a reader cannot tell whether it was. A final else, even one with only a comment, says that it was considered.

Reported is the last if of a chain with at least one else if and no final else. Many chains do nothing in the remaining case by design, so the rule runs only when every rule is enabled.

From the rule's source, else_if_without_else.rs.

Example from the rule's tests
package p func f(n int) string {	if n < 0 {		return "negative"	} else if n == 0 {tx0 reports: this if-else-if chain has no final else		return "zero"	}	if n > 10 {		return "big"	} else if n > 5 {		return "medium"	} else {		return "small"	}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:else-if-without-else" = "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-if-without-else] reason="..."

tx0 explain tx0/go:else-if-without-else shows the same in the terminal. Found a false positive? Report it.