GeezerVison is showing github.com — [java] Fix #6435: UnconditionalIfStatement flags arbitrarily negated boolean literals by fudianchn · Pull Request #6939 · pmd/pmd
Conversation
fudianchn
commented
Aug 5, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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.
|
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)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…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
changed the title
[java] Fix #6435: UnconditionalIfStatement flags negated boolean constant
[java] Fix #6435: UnconditionalIfStatement flags arbitrarily negated boolean literals
Aug 11, 2026
|
@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).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…negated-6435
# Conflicts:
# docs/pages/release_notes.md
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|