A condition that reads local variables has the same value on every path that reaches it
What it finds
``java int attempts = 0; ... if (attempts > MAX) { // attempts is still 0 here on every path ``
A condition that comes out the same on every path makes one of its branches dead code, and usually means that an update of a variable was lost or the wrong variable is tested.
The rule folds the constants that local variables hold through the shared IR, path by path, taking a branch whose condition is known as the only way on. Reported is a condition of an if, a loop, a conditional expression or a when guard, or an operand of &&, || or ! in one, that reads a local variable or parameter and has the same value wherever it is reached. Conditions made of literals and constants alone, as while (true) or if (DEBUG), are meant and left alone, and so are those that read only local variables set once to a literal, which serve the same purpose. A finally block is reported only when the condition is the same in every copy the lowering makes of it. Assertions are not conditions here: assert false marks code thought unreachable, which runs on when assertions are off.
From the rule's source, constant_condition.rs.
class Retry { static final boolean DEBUG = false; void run(Task task) { int retries = 3; if (task.flaky()) { retries = 3; } if (retries > 3) {tx0 reports: retries > 3 is always false here task.stop(); } if (DEBUG) { task.log(); } while (true) { if (task.done()) { break; } } } void loop(Task task) { boolean first = true; for (int i = 0; i < 3; i++) { if (!first) { task.log(); } first = false; } }} 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:constant-condition" = "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:constant-condition] reason="..."tx0 explain tx0/java:constant-condition shows the same in the terminal. Found a false positive? Report it.