An if-else-if chain ends without an else

tx0/java:if-chain-without-elsestrictwarningstyle

What it finds

A chain of if and else if without a final else leaves open what happens when no test holds: deliberately nothing, or a case that was forgotten. An else, even one with a comment, says which.

Reported is the last else if of a chain that has no final else.

From the rule's source, if_chain_without_else.rs.

Example from the rule's tests
class Grades {  String grade(int score) {    String result = "";    if (score > 90) {      result = "A";    } else if (score > 80) {tx0 reports: has no final else      result = "B";    }    if (score > 50) {      result += "+";    } else if (score > 40) {      result += "-";    } else {      result += "?";    }    return result;  }}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

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

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