Skip to content

[java] UnusedAssignment: False positive for an assignment in a condition operand that may be skipped #7018

Closed

Description

@fudianchn

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):

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

  1. added a commit that references this issue on Oct 9, 2026
    eaee674
  2. changed the title [-][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[/+] on Oct 23, 2026
  3. added
    a:false-positive
    on Oct 23, 2026
  4. added a commit that references this issue on Oct 23, 2026
  5. added this to the 7.29.0 milestone on Oct 23, 2026
  6. added a commit that references this issue on Oct 23, 2026
    0a48165
  7. changed the title [-][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[/+] on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions