An if-else-if chain has no final else for the case where no condition holds
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.
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" }} 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:
[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.