Repository navigation
[java] Fix #5940: UnusedAssignment FP when constant operand short-circuits the condition - #7016
Merged
UncleOwen merged 4 commits intoSep 8, 2026
Conversation
fudianchn
marked this pull request as draft
August 31, 2026 14:26
fudianchn
force-pushed
the
fix/unusedassignment-conditional-fp-5940
branch
from
August 31, 2026 14:26
bcf47b5 to
af3f7a6
Compare
fudianchn
marked this pull request as ready for review
August 31, 2026 14:28
fudianchn
marked this pull request as draft
August 31, 2026 14:35
fudianchn
force-pushed
the
fix/unusedassignment-conditional-fp-5940
branch
from
August 31, 2026 14:35
af3f7a6 to
1e1e321
Compare
fudianchn
marked this pull request as ready for review
August 31, 2026 14:36
Contributor
|
Compared to main: (comment created at 2026-09-08 13:10:46+00:00 for 110842a) |
fudianchn
marked this pull request as draft
August 31, 2026 15:13
fudianchn
force-pushed
the
fix/unusedassignment-conditional-fp-5940
branch
from
August 31, 2026 15:30
1e1e321 to
2ce3004
Compare
fudianchn
marked this pull request as ready for review
August 31, 2026 15:30
fudianchn
marked this pull request as draft
August 31, 2026 15:42
fudianchn
force-pushed
the
fix/unusedassignment-conditional-fp-5940
branch
from
August 31, 2026 15:43
2ce3004 to
00138f3
Compare
fudianchn
marked this pull request as ready for review
August 31, 2026 15:43
UncleOwen
reviewed
Sep 7, 2026
fudianchn
marked this pull request as draft
September 8, 2026 04:18
…circuits the condition Signed-off-by: 付典 <[email protected]>
fudianchn
force-pushed
the
fix/unusedassignment-conditional-fp-5940
branch
from
September 8, 2026 04:21
00138f3 to
a1b4165
Compare
fudianchn
marked this pull request as ready for review
September 8, 2026 04:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.
Describe the PR
DataflowPassnow 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 ofok2was reported as unused although the reassignment inif (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 aContextedAssertionErroroncom.sun.java.util.jar.pack.Attributein the openjdk-11 regression corpus (1 new error, and 20 LawOfDemeter findings of that file lost to error recovery). The regression test inLawOfDemeter.xmlpins this.The prune applies in condition position (if/while/ternary), where
linkConditionalmodels 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, althoughflag == falseshort-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?
./mvnw clean verifypasses (checked automatically by github actions)Behavior delta
reportUnusedVariables=truethere 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).reportUnusedVariables=trueaddition); corpus delta over java-regression-tests@main, checkstyle-9.1src/main/java, PMD's own sources and the jdk-11+28Attributefile is exactly the FP sample itself; 0 unclassified deltas.Unit matrix and corpus evidence
if (false && (ok2 = false))(issue sample)if (true || (ok = false))if (1 == 2 && (ok = false))if (!true && (ok = false))if ((false) && (ok = false))if (false && true && (ok = false))if (flag || false && (ok = false))if (DEBUG && (ok = false))withstatic final boolean DEBUG = falseFoo() { this.f = 1; if (false && (this.f = 2) == 2) ... }(field)while (false && (ok = false))false && (x = 2) == 2 ? 1 : 2(ternary)if (false || (ok = false))if (true && (ok = false))if (false && true || (ok = false))if (true || f2(h.getItem()))with initialized localh(LawOfDemeter)int x = 1; if (false && x > 0)withreportUnusedVariables=trueThe 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 withContextedAssertionError: 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,InvalidLogMessageFormatwere run before/after (same driver,mainversion ofDataflowPassfirst on the classpath for the before run) over java-regression-tests@main (50 files), checkstyle-9.1src/main/java(412 files), PMD's ownpmd-core/pmd-javasources, and the jdk-11+28com.sun.java.util.jar.pack.Attributefile 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);Attributeyields 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 thereportUnusedVariables=trueaddition 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
ifwith an else branch, a normally completing then branch,while/forconditions, a ternary condition anddo {} while (...)all stay clean on main; the report that remains ondo { b = true; } while (false && (b = false));is a true positive, since the body assignment kills the initializer regardless of the condition.