The condition of an if, loop or ?: is an assignment rather than a comparison
What it finds
if (done = true) assigns instead of comparing: the condition is always the assigned value, and the variable changes as a side effect. Java accepts it only for boolean variables, where == or the variable alone was meant.
Reported when the whole condition of an if, while, do or conditional expression is a plain assignment; an assignment compared with something, as in while ((line = reader.readLine()) != null), is left alone.
From the rule's source, assignment_as_condition.rs.
class Flags { boolean done; void check(boolean next, java.io.BufferedReader r) throws java.io.IOException { if (done = next) {tx0 reports: done = next assigns done = false; } String line; while ((line = r.readLine()) != null) { done = line.isEmpty(); } boolean x = (done = true) ? next : done;tx0 reports: done = true assigns }} 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:assignment-as-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:assignment-as-condition] reason="..."tx0 explain tx0/java:assignment-as-condition shows the same in the terminal. Found a false positive? Report it.