Repository navigation
[java] Fix #7018: UnusedAssignment false positive for assignment in condition operand - #7067
Merged
Merged
Conversation
Contributor
|
Compared to main: (comment created at 2026-10-23 12:32:31+00:00 for 17cf4f1) |
UncleOwen
reviewed
Oct 8, 2026
…n condition operand ReachingDefsVisitor#visitShortcutOrExpr evaluates the operands of a shortcut condition in the state that may alias the branch state of an if without else (makeConditional reuses before as the else state in that case). The strong updates of the right operand therefore destroy, in place, the reaching defs of the outcome on which the left operand alone decided the condition and the right operand was skipped. The statements after the if only saw the mutated state, in which the initializer was killed by an assignment that may not have executed. Snapshot the reaching defs before evaluating the right operand and merge them back when thenState aliases that shared state and the left operand does not force the evaluation of the right operand, so the skip outcome survives the join. Constant left operands keep the existing dead-operand and forced-evaluation handling. Fixes pmd#7018 Signed-off-by: 付典 <[email protected]>
fudianchn
force-pushed
the
fix/unusedassignment-condition-rhs
branch
from
October 9, 2026 02:09
0ca70f5 to
eaee674
Compare
UncleOwen
reviewed
Oct 9, 2026
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
if (flag() && (ok = false))reportedThe initializer for variable 'ok' is never usedalthough the path whereflag()returns false skips the assignment and reads the initializer.Root cause:
makeConditionalreusesbeforeas the else state when the if has no else branch. The right operand is evaluated in that shared state, so its strong update destroys the reaching defs of the outcome where the left operand alone decided the condition.Fix: snapshot the reaching defs before evaluating the right operand and merge them back when
thenStatealiases the shared state and the left operand does not force the evaluation. Constant left operands keep the dead-operand and forced-evaluation handling of #5940.Known boundary: with an assignment in the left subexpression of an outer
&&and an abruptly completing then branch (flag() && (ok = false) && (ok2 = true)), one false positive remains; this fix reduces it from two.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)