A condition repeats a test that has already decided it on every path
What it finds
``java if (count > limit) { ... if (count > limit) { // always true: nothing between changed either ``
A test that an earlier test on every path to it has already settled is either dead weight or a sign that the wrong variable or operator is used; when it is always false, the code it guards never runs.
The rule tracks, through the shared IR, which comparisons are known to hold on every path to a point: a < b, a == b, x instanceof T and boolean variables, learned from the branches taken. A local variable is a new value at every assignment, so an assignment in between ends what is known about it, and fields and method results are read afresh each time and never known. Reported is a condition whose comparison, or its mirror image (b > a for a < b, != for ==), was already decided, and an equality with a constant where the value is known to equal another constant. An assert decides nothing, since repeating its test in an if for when assertions are off is common practice. A finally block is reported only when every copy the lowering makes of it is decided the same way.
From the rule's source, condition_already_decided.rs.
class Limits { void check(int count, int limit, Object o) { if (count > limit) { warn(); if (limit < count) {tx0 reports: limit < count is always true here, as an earlier test decided warn(); } } if (o == null) { return; } if (o != null) {tx0 reports: o != null is always true here, as an earlier test decided warn(); } } void fin(boolean run, int added, int needed) { try { if (run) { warn(); } } catch (Throwable t) { warn(); } finally { if (run) { warn(); } } try { while (added < needed) { warn(); added += 1; } } finally { if (added < needed) { warn(); } } } void changed(int count, int limit) { if (count > limit) { count = 0; if (count > limit) { warn(); } } } void warn() {}} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:condition-already-decided" = "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:condition-already-decided] reason="..."tx0 explain tx0/java:condition-already-decided shows the same in the terminal. Found a false positive? Report it.