Skip to content

[java] Fix #5159: UnusedAssignment: an assert statement may not run - #7104

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
Eljees:fix/5159-unused-assignment-assert
Sep 25, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
Eljees:fix/5159-unused-assignment-assert

Conversation

@Eljees

@Eljees Eljees commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

UnusedAssignment assumed that an assert statement always runs, so an assignment made inside one was treated as always overwriting the variable. Assertions are disabled by default at runtime, and #5159 shows the idiom that depends on it:

boolean assertionsAreEnabled = false;
assert assertionsAreEnabled = true;
if (assertionsAreEnabled) { ... }

Here the rule reported false as never used, although it is exactly the value read when assertions are off.

DataflowPass had no case for ASTAssertStatement, so the default child-by-child visit applied. This adds one: the condition and the detail message are visited in a forked state, which is then joined with the state before the statement, the same way an if without else is handled. Reads inside the assert are still recorded as usages, so nothing that was used before becomes unused.

Tests

Three cases in UnusedAssignment.xml:

  • the code from [java] UnusedAssignment: False positive when using assert #5159: no violation;
  • an assignment in the assert's detail message (assert n > 0 : reason = "negative";), which only runs when the assertion is enabled and fails: no violation for the initializer;
  • a value overwritten after an unrelated assert: still reported, so the assert does not hide real dead stores.

Without the change the first two fail (Expected 0 problem(s), 1 problem(s) found). With it, UnusedAssignmentTest, UnusedLocalVariableTest, ImmutableFieldTest and SingularFieldTest (the rules built on DataflowPass) pass: 353 tests, 0 failures.

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)

Assertions are disabled by default, so the dataflow pass must not treat an
assert as always evaluated. Visit its condition and detail message in a
forked state and join it with the state before the statement.

Fixes pmd#5159
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 24, 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 10 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-25 20:44:52+00:00 for b082d30)

@UncleOwen UncleOwen changed the title [java] UnusedAssignment: an assert statement may not run [java] Fix #5159: UnusedAssignment: an assert statement may not run Sep 25, 2026
@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 25, 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 983d42e into pmd:main Sep 25, 2026
13 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 using assert

2 participants