Repository navigation
[java] Fix #6435: UnconditionalIfStatement flags arbitrarily negated boolean literals - #6939
Conversation
|
Compared to main: (comment created at 2026-08-11 18:03:31+00:00 for 81c7922) |
|
If we fix this, I think we should fix it in a way that also handles |
…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]>
f2f536e to
14cb000
Compare
|
@UncleOwen Updated the condition matcher to accept a |
…negated-6435 # Conflicts: # docs/pages/release_notes.md
Describe the PR
UnconditionalIfStatementnow reports anifcondition 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 oneUnaryExpression, so!!trueand 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 aBooleanLiteralor aUnaryExpressionwhose operator is!. This expresses the completeUnaryExpression(!)* -> BooleanLiteralshape without a fixed maximum depth. Conditions ending in a variable, such as!band!!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.
UnconditionalIfStatementitself 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 changedUnusedReturnValueviolations, 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?
./mvnw clean verifypasses (checked automatically by github actions)