Skip to content

[java] Fix #6435: UnconditionalIfStatement flags arbitrarily negated boolean literals - #6939

Merged
UncleOwen merged 2 commits into
pmd:mainfrom
fudianchn:fix/unconditionalifstatement-negated-6435
Aug 11, 2026
Merged

UncleOwen merged 2 commits into
pmd:mainfrom
fudianchn:fix/unconditionalifstatement-negated-6435

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 5, 2026 •

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

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)

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 9, 2026 •

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)

@UncleOwen

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.

@fudianchn
fudianchn marked this pull request as draft August 11, 2026 08:46
…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

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).

…negated-6435

# Conflicts:
#	docs/pages/release_notes.md
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 11, 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 eb9dc7d into pmd:main Aug 11, 2026
11 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] UnconditionalIfStatement: False negative for negated boolean constant

2 participants