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 #6435: UnconditionalIfStatement flags arbitrarily negated boolean literals by fudianchn · Pull Request #6939 · pmd/pmd

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 5, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Describe the PR

UnconditionalIfStatement now reports an if condition when a boolean literal is wrapped in any number of logical ! operators. The previous XPath handled a direct literal and the initial fix handled exactly one UnaryExpression, so !!true and longer chains still escaped the rule.

The XPath now inspects only the if-condition (*[1]) and accepts it when every node in that expression subtree is either a BooleanLiteral or a UnaryExpression whose operator is !. This expresses the complete UnaryExpression(!)* -> BooleanLiteral shape without a fixed maximum depth. Conditions ending in a variable, such as !b and !!b, remain excluded, as do conditions containing other expression node types.

Regression coverage now spans zero, one, two, three, and eight logical negations, a parenthesized chain, non-literal chains, and an if-body scoping guard. The new repeated-negation test fails against the previous PR head with expected: <4> but was: <0> and passes with the structural XPath.

Local verification:

  • ./mvnw -o -pl pmd-java -am test -Dtest=UnconditionalIfStatementTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false: 13 tests, 0 failures, 0 errors.
  • ./mvnw clean verify: BUILD SUCCESS.

Regression tester: compared to main, the changeset introduces 0 new violations, 0 new errors, and 0 new configuration errors. UnconditionalIfStatement itself reports the same violations on every project in the corpus (no added, removed, or changed, e.g. 949 to 949 on checkstyle and 4 to 4 on spring-framework), so the broader matcher does not flag any new site in openjdk-11, spring-framework, checkstyle, or the other projects. The remaining deltas are 8 changed UnusedReturnValue violations, a rule this PR does not touch, plus baseline errors and configuration errors that are removed on this base; none of these are introduced by this XPath-only change to one rule.

Related issues

  • Fix [java] UnconditionalIfStatement: False negative for negated boolean constant #6435

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
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 9, 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 8 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 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-08-11 18:03:31+00:00 for 81c7922)

All reactions

Sorry, something went wrong.

Uh oh!

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

@UncleOwen

UncleOwen commented Aug 10, 2026

Copy link
Copy Markdown
Member

If we fix this, I think we should fix it in a way that also handles if(!!true). And any number of additional ! as well.

All reactions

Sorry, something went wrong.

Uh oh!

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

@fudianchn
fudianchn marked this pull request as draft August 11, 2026 08:46
@fudianchn
[java] Fix pmd#6435: UnconditionalIfStatement flags arbitrarily negat…
14cb000
…ed boolean literals

UnconditionalIfStatement only matched direct BooleanLiteral conditions and,
in the initial fix, a single UnaryExpression wrapping one. This still missed
equivalent conditions such as !!true and longer logical-negation chains.

Match an if condition when every node in its expression subtree is either a
BooleanLiteral or a UnaryExpression with operator '!'. This handles any
number of logical negations without fixed-depth enumeration, while chains
ending in a variable or containing another expression node remain excluded.

Add regression coverage for single, repeated, deeply nested, and
parenthesized negations, plus non-literal negative cases.

Signed-off-by: 付典 <[email protected]>
@fudianchn
fudianchn force-pushed the fix/unconditionalifstatement-negated-6435 branch from f2f536e to 14cb000 Compare August 11, 2026 10:46
@fudianchn fudianchn changed the title [java] Fix #6435: UnconditionalIfStatement flags negated boolean constant [java] Fix #6435: UnconditionalIfStatement flags arbitrarily negated boolean literals Aug 11, 2026
@fudianchn
fudianchn marked this pull request as ready for review August 11, 2026 11:14
@fudianchn

fudianchn commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen Updated the condition matcher to accept a BooleanLiteral wrapped by any number of logical ! nodes. The XPath now checks every node in the if-condition subtree, so !!true, !!!false, and a deeper eight-operator case are reported without a fixed maximum depth, while chains ending in a variable such as !b and !!b remain excluded. Coverage spans zero, one, two, three, and eight negations, plus a parenthesized chain and non-literal cases. UnconditionalIfStatementTest is 13/13, ./mvnw clean verify passes, and all PR checks including dogfood and regressiontester are green. The regression report shows 0 new violations, 0 new errors, and 0 new configuration errors, with no added, removed, or changed UnconditionalIfStatement violations on any project (949 to 949 on checkstyle, 4 to 4 on spring-framework).

All reactions

Sorry, something went wrong.

Uh oh!

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

@UncleOwen
Merge branch 'main' into fork/fudianchn/fix/unconditionalifstatement-…
81c7922
…negated-6435

# Conflicts:
#	docs/pages/release_notes.md
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 11, 2026
UncleOwen
UncleOwen approved these changes Aug 11, 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 eb9dc7d into pmd:main Aug 11, 2026
11 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.27.0

Development

Successfully merging this pull request may close these issues.

[java] UnconditionalIfStatement: False negative for negated boolean constant

Uh oh!

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

2 participants

@fudianchn @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.