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] Fix #5159: UnusedAssignment: an assert statement may not run by Eljees · Pull Request #7104 · pmd/pmd

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

  • Fixes [java] UnusedAssignment: False positive when using assert #5159

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)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Eljees
[java] UnusedAssignment: an assert statement may not run
da012cd
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 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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 added 2 commits September 25, 2026 22:17
@UncleOwen
Merge branch 'main' into fork/Eljees/fix/5159-unused-assignment-assert
8025778
@UncleOwen
[docs] Update release notes
b082d30
UncleOwen
UncleOwen approved these changes Sep 25, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit 983d42e into pmd:main Sep 25, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.29.0

Development

Successfully merging this pull request may close these issues.

[java] UnusedAssignment: False positive when using assert

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Eljees @UncleOwen
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.
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.