GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] UnusedAssignment: False positive for an assignment in a condition operand that may be skipped · Issue #7018 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#7067
Closed
[java] UnusedAssignment: False positive for an assignment in a condition operand that may be skipped#7018
#7067
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.29.0

Description

@fudianchn
fudianchn
opened on Aug 31, 2026
Issue body actions

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 else branch, a while/ternary condition instead of the if: 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

  1. fudianchn
    added a commit that references this issue on Oct 9, 2026

    [java] Fix pmd#7018: UnusedAssignment false positive for assignment i…

    This issue will close once commit eaee674 is merged into the 'main' branch.
    eaee674
  2. adangel
    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. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Oct 23, 2026
  4. adangel
    added a commit that references this issue on Oct 23, 2026

    Update release notes (pmd#7018)

    17cf4f1
  5. adangel
    added this to the 7.29.0 milestone on Oct 23, 2026
  6. adangel
    closed this as completedin #7067on Oct 23, 2026
  7. adangel
    added a commit that references this issue on Oct 23, 2026

    [java] Fix pmd#7018: UnusedAssignment false positive for assignment i…

    This issue will close once commit 0a48165 is merged into the 'main' branch.
    0a48165
  8. adangel
    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-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.29.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.