A condition of an if-else chain repeats an earlier one
What it finds
if (a) {} else if (a) {}: a condition that repeats an earlier one of the same chain is false whenever it is reached, so its branch never runs. Usually one of the two was meant to test something else.
Between two conditions of a chain only conditions run. The chain is followed only as long as no condition can change what a later one reads: it ends at a condition that calls a method, creates an object, assigns or increments.
From the rule's source, duplicate_condition.rs.
class Dispatch { int route(int code, String name, java.util.Iterator<String> it) { if (code == 1) { return 1; } else if (code == 2) { return 2; } else if (code == 1) {tx0 reports: the condition code == 1 repeats an earlier one return 3; } if (name == null) { return 0; } else if ((name == null)) {tx0 reports: the condition name == null repeats an earlier one return 1; } if (it.hasNext()) { it.next(); } else if (it.hasNext()) { return 4; } if (code > 0) { return 5; } if (code > 0) { return 6; } return 7; }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:duplicate-condition" = "off"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:duplicate-condition] reason="..."tx0 explain tx0/java:duplicate-condition shows the same in the terminal. Found a false positive? Report it.