Repository navigation
Description
AI disclosure: this issue was prepared with AI coding agents, reviewed and revised line by line by me.
Affects PMD Version:
7.27.0 and current main (9c1fe8ff, 7.28.0-SNAPSHOT), reproduced on both
Rule:
UnusedAssignment
Description:
An assignment in the right operand of a && condition is treated as if it always executes, although short-circuit evaluation may skip it. The initializer is reported as unused even though the path where flag() returns false skips the assignment and reads the initializer after the if.
Code Sample demonstrating the issue:
public class Foo {
static boolean flag() {
return Math.random() < 0.5;
}
public void process() {
boolean ok = true;
if (flag() && (ok = false)) {
return;
}
System.out.println(ok); // prints true when flag() is false
}
}Expected outcome:
PMD reports a violation at line 10 (The initializer for variable 'ok' is never used (overwritten on line 11)), but that's wrong. That's a false positive.
Nearby shapes that behave correctly (checked on 7.27.0 and main):
if (flag() || (ok = false)) { return; }: the fall-through implies the right operand was evaluated, so the report is correct there.- A normally completing then branch, an added
elsebranch, awhile/ternary condition instead of theif: no report. if (false && (ok = false)): the constant case, fixed by [java] Fix #5940: UnusedAssignment FP when constant operand short-circuits the condition #7016 (issue [java] UnusedAssignment: False positive when assignment is in conditional statement #5940). This report is the non-constant remainder of the same family.
Running PMD through: CLI
Root cause (from reading DataflowPass): in ReachingDefsVisitor#visitShortcutOrExpr (pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/internal/DataflowPass.java:567 on main) the shortcut operands are evaluated in cur, which starts as the before state object itself, so the assignment in the right operand (line 570) strong-updates before in place. When the then branch of the enclosing if completes abruptly and there is no else branch, the branch fork that still holds the initializer (unioned with the assignment) never merges back into the result, so the statements after the if only see the mutated state, in which the initializer was killed by an assignment that may not have executed.
Fix direction: the no-else fall-through state should account for the paths on which the right operand was not evaluated, e.g. by evaluating the shortcut operands in a fork of before instead of in before itself, while still recording the definitions for the paths where the operand is evaluated.
Activity
[java] Fix pmd#7018: UnusedAssignment false positive for assignment i…
[-][java] UnusedAssignment false positive for an assignment in a condition operand that may be skipped[/-][+][java] UnusedAssignment: false positive for an assignment in a condition operand that may be skipped[/+]Update release notes (pmd#7018)
[java] Fix pmd#7018: UnusedAssignment false positive for assignment i…
[-][java] UnusedAssignment: false positive for an assignment in a condition operand that may be skipped[/-][+][java] UnusedAssignment: False positive for an assignment in a condition operand that may be skipped[/+]