Skip to content

[java] Fix #5940: UnusedAssignment FP when constant operand short-circuits the condition - #7016

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
fudianchn:fix/unusedassignment-conditional-fp-5940
Sep 8, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
fudianchn:fix/unusedassignment-conditional-fp-5940

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.

Describe the PR

DataflowPass now stops processing the right operand of a &&/|| when the left operand is a compile-time constant that already decides the value of the condition (JLS 15.23.2, 15.24.2). Such an operand is never evaluated, so the assignments it contains do not happen and cannot kill earlier definitions. This removes the reported false positive: the initializer of ok2 was reported as unused although the reassignment in if (false && (ok2 = false)) never executes.

The references of the skipped operand still get their reaching definitions recorded, computed from the state that reaches the operand. Without this, rules that query reaching definitions hit the fallback of DataflowPass.getReachingDefinitions, whose contract only covers final fields and blank locals: on the first push this crashed LawOfDemeter with a ContextedAssertionError on com.sun.java.util.jar.pack.Attribute in the openjdk-11 regression corpus (1 new error, and 20 LawOfDemeter findings of that file lost to error recovery). The regression test in LawOfDemeter.xml pins this.

The prune applies in condition position (if/while/ternary), where linkConditional models short-circuiting. Assignments in short-circuited operands in other expression positions (e.g. boolean r = false && (b = false);) are still over-approximated: modeling shortcut conditionals in every position needs its own control-flow treatment, which is out of scope here. I can prepare a follow-up if this direction is of interest. The same false positive also exists with a non-constant left operand when the then branch completes abruptly without an else branch: if (flag && (b = false)) { return; } use(b); reports the initializer as unused both on main and with this PR, although flag == false short-circuits and keeps the initializer. Covering that variant needs the no-else fall-through merge to account for paths on which the right operand was not evaluated, an engine-level change that is out of scope here (reported as #7018).

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Behavior delta

  1. Intent: assignments in a condition operand that a compile-time constant short-circuits away are no longer treated as executing. In the default configuration the change can only remove reports. With reportUnusedVariables=true there is one addition family: an initializer whose only read lies inside the short-circuited operand is now reported, since that read never executes and the variable really is only initialized and never read (on main the dead read still counts as a use).
  2. Delta: unit matrix of 16 new test-codes two-state green (9 fix encoders report the FP on main, 6 guards keep the correct behavior, 1 opt-in encoder pins the reportUnusedVariables=true addition); corpus delta over java-regression-tests@main, checkstyle-9.1 src/main/java, PMD's own sources and the jdk-11+28 Attribute file is exactly the FP sample itself; 0 unclassified deltas.
  3. Recompute: the collapsed table below lists the matrix; the Regression Tester report on this PR covers the full corpus, I will cross-check it when it posts.
Unit matrix and corpus evidence
test kind expected main this PR
if (false && (ok2 = false)) (issue sample) fix encoder 0 1 0
if (true || (ok = false)) fix encoder 0 1 0
if (1 == 2 && (ok = false)) fix encoder 0 1 0
if (!true && (ok = false)) fix encoder 0 1 0
if ((false) && (ok = false)) fix encoder 0 1 0
if (false && true && (ok = false)) fix encoder 0 1 0
if (flag || false && (ok = false)) fix encoder 0 1 0
if (DEBUG && (ok = false)) with static final boolean DEBUG = false fix encoder 0 1 0
Foo() { this.f = 1; if (false && (this.f = 2) == 2) ... } (field) fix encoder 0 1 0
while (false && (ok = false)) guard 0 0 0
false && (x = 2) == 2 ? 1 : 2 (ternary) guard 0 0 0
if (false || (ok = false)) guard 1 @l3 1 1
if (true && (ok = false)) guard 1 @l3 1 1
if (false && true || (ok = false)) guard 1 @l3 1 1
if (true || f2(h.getItem())) with initialized local h (LawOfDemeter) guard 1 @L13 1 1
int x = 1; if (false && x > 0) with reportUnusedVariables=true opt-in addition 1 @l3 0 1

The three UnusedAssignment guards expecting a violation pin the evaluated-operand cases (false ||, true &&, constant-false disjunction). With a value-blind mutant that prunes on any constant left operand, exactly these three fail (expected 1, got 0), so they are load-bearing. The LawOfDemeter guard pins the recording of reaching definitions in the skipped operand: a prune that skips exploration without recording fails it with ContextedAssertionError: Should be a blank local variable (the state of the first push), main and this PR both pass it.

Corpus check: UnusedAssignment, ImmutableField, SingularField, LawOfDemeter, AvoidThrowingNullPointerException, ReturnEmptyCollectionRatherThanNull, ImplicitSwitchFallThrough, InvalidLogMessageFormat were run before/after (same driver, main version of DataflowPass first on the classpath for the before run) over java-regression-tests@main (50 files), checkstyle-9.1 src/main/java (412 files), PMD's own pmd-core/pmd-java sources, and the jdk-11+28 com.sun.java.util.jar.pack.Attribute file that produced the regression tester error: 1946 vs 1945 violations, 0 errors on both sides, the only diff is the #5940 sample itself (probe file); Attribute yields the identical 26 findings on both sides. No auxclasspath was configured, so absolute counts can differ from the Regression Tester run, but the before/after comparison is symmetric. The corpus runs use the default configuration, so the reportUnusedVariables=true addition family is not visible there.

FP boundary (probe-verified): the false positive is observable when the then branch completes abruptly and there is no else branch. An if with an else branch, a normally completing then branch, while/for conditions, a ternary condition and do {} while (...) all stay clean on main; the report that remains on do { b = true; } while (false && (b = false)); is a true positive, since the body assignment kills the initializer regardless of the condition.

@fudianchn
fudianchn marked this pull request as draft August 31, 2026 14:26
@fudianchn
fudianchn force-pushed the fix/unusedassignment-conditional-fp-5940 branch from bcf47b5 to af3f7a6 Compare August 31, 2026 14:26
@fudianchn
fudianchn marked this pull request as ready for review August 31, 2026 14:28
@fudianchn
fudianchn marked this pull request as draft August 31, 2026 14:35
@fudianchn
fudianchn force-pushed the fix/unusedassignment-conditional-fp-5940 branch from af3f7a6 to 1e1e321 Compare August 31, 2026 14:35
@fudianchn
fudianchn marked this pull request as ready for review August 31, 2026 14:36
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-08 13:10:46+00:00 for 110842a)

@fudianchn
fudianchn marked this pull request as draft August 31, 2026 15:13
@fudianchn
fudianchn force-pushed the fix/unusedassignment-conditional-fp-5940 branch from 1e1e321 to 2ce3004 Compare August 31, 2026 15:30
@fudianchn
fudianchn marked this pull request as ready for review August 31, 2026 15:30
@fudianchn
fudianchn marked this pull request as draft August 31, 2026 15:42
@fudianchn
fudianchn force-pushed the fix/unusedassignment-conditional-fp-5940 branch from 2ce3004 to 00138f3 Compare August 31, 2026 15:43
@fudianchn
fudianchn marked this pull request as ready for review August 31, 2026 15:43
@fudianchn
fudianchn marked this pull request as draft September 8, 2026 04:18
@fudianchn
fudianchn force-pushed the fix/unusedassignment-conditional-fp-5940 branch from 00138f3 to a1b4165 Compare September 8, 2026 04:21
@fudianchn
fudianchn marked this pull request as ready for review September 8, 2026 04:21
@UncleOwen UncleOwen added this to the 7.28.0 milestone Sep 8, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit a7a533d into pmd:main Sep 8, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UnusedAssignment: False positive when assignment is in conditional statement

2 participants