Skip to content

[java] Fix #6694: Trigger SimplifyBooleanReturns when expressions require parentheses - #6922

Merged
UncleOwen merged 17 commits into
pmd:mainfrom
Will-6543:issue-6694
Sep 8, 2026
Merged

UncleOwen merged 17 commits into
pmd:mainfrom
Will-6543:issue-6694

Conversation

@Will-6543

@Will-6543 Will-6543 commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

Changes behaviour of SimplifyBooleanReturns to trigger for expressions which would require parentheses when simplified but are not currently parenthesised.

Old Behaviour - rule only triggers if both expressions can be directly placed into a simplified expression (using De Morgan's if required)
New Behaviour - in these cases where adding parentheses would cause the old rule to trigger the rule is triggered regardless and the resulting message includes the required parentheses.

Tests changed in accordance with this change if it were to be implemented.

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)

@kdelay kdelay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I checked this out locally and ran the tests, since CI does not seem to have run on the PR yet.

Branch issue-6694 (26968bd), JDK 17, ./mvnw -pl pmd-java -am test: 9783 tests, 0 failures, 21 skipped.

Behaviour against the report. I dropped the sample from #6694 into SimplifyBooleanReturns.xml as a throwaway test-code:

public class FactoryChecker {
    public boolean isFactoryMethodA(String qualifier, String name) {
        if (qualifier != null) {
            return typeEndsWith(qualifier, "Factory") || nameEndsWith(name);
        }
        return false;
    }

    public boolean isFactoryMethodB(String qualifier, String name) {
        if (qualifier != null) {
            return (typeEndsWith(qualifier, "Factory") || nameEndsWith(name));
        }
        return false;
    }
    // ...
}

On main that produces 1 violation (only isFactoryMethodB, the parenthesized one). On this branch it produces 2:

snippet.java:3:  SimplifyBooleanReturns: This if statement can be replaced by `return {condition} && ({thenBranch});`
snippet.java:10: SimplifyBooleanReturns: This if statement can be replaced by `return {condition} && {thenBranch};`

The reported inconsistency is gone, and the two messages differ only by the parentheses the unparenthesized form needs.

Two things I would add:

  1. That sample is worth keeping as a regression test. The file already names cases after their issue (#3852, #4763), and all five of the new cases use the if (c) { ... } else { ... } shape, so they reach getElseExpr through node.getElseBranch(). The report is about the if (c) return X; return false; shape, which takes the getNextSibling() fallback instead. It is the same code path afterwards, but the reported form is currently untested.

  2. docs/pages/release_notes.md has no entry for #6694. Recent rule fixes add one under ### 🐛️ Fixed Issues / java in the same PR. Since the rule now reports on code it deliberately stayed silent on before, an entry under "Changed Rules" may also fit; that is probably a maintainer call.

Minor, all in needsToBeReportedWhenOneBranchIsBoolean:

  • !doesNotNeedNewParensUnderInfix(...) is a double negative and now appears twice. A positively named local (boolean needsParensUnder(ASTExpression, BinaryOp)) would read easier at the call sites.
  • conditionNegated && needsNewParensWhenNegating(condition) || !doesNotNeedNewParensUnderInfix(condition, op) leans on && binding tighter than ||. Explicit grouping would make the intent obvious.
  • Leftover blank lines: three in a row before the return, and one left where the old early return was.

@Will-6543

Copy link
Copy Markdown
Contributor Author

Thank you for your suggestions kdelay, they are now implemented.
For the release notes I have just put it in ### 🐛️ Fixed Issues/java-design for now, I'll leave the decision of whether or not it should go in #### Changed Rules up to a maintainer.

@pmd-actions-helper

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

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 13 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-31 17:03:24+00:00 for 51ed211)

@UncleOwen

Copy link
Copy Markdown
Member

I always find it problematic when existing tests are changed. Have you checked the Git history and the old pull requests and issues (here on GitHub and on SourceForge) to see if there’s a rationale for why this was done this way?

@Will-6543

Copy link
Copy Markdown
Contributor Author

I always find it problematic when existing tests are changed. Have you checked the Git history and the old pull requests and issues (here on GitHub and on SourceForge) to see if there’s a rationale for why this was done this way?

#3786 was where it was first introduced with the rationale that the parentheses that would need to be added are ugly, however this introduces the inconsistency mentioned in this issue (#6694)

Perhaps a rule property which enables/disables triggering the rule when additional parentheses are required would be in order?

@UncleOwen

Copy link
Copy Markdown
Member

Before:

    public boolean startsWith(String prefix, int fromIndex) {
        if (fromIndex < 0 || fromIndex + prefix.length() > len) {
            return false;
        }
        return str.startsWith(prefix, idx(fromIndex));
    }

After:

    public boolean startsWith(String prefix, int fromIndex) {
        return !(fromIndex < 0 || fromIndex + prefix.length() > len) && str.startsWith(prefix, idx(fromIndex));
    }

These changes make the code less readable, not more.

#6694 mentions two possible ways to fix this inconsistency. I think we should go with NOT triggering a violation.

@Will-6543

Copy link
Copy Markdown
Contributor Author

It now does not trigger on the case which has parentheses which cannot be removed.
Old testcases had to be changed to facilitate this.
In particular the case from #3852 now does not trigger when it can still functionally be replaced by return true
However triggering here would require simplifying all boolean literal expressions (true || false simplifying to true and therefore not needing parentheses) which is out of scope for this issue I would say.

@UncleOwen UncleOwen added this to the 7.28.0 milestone Sep 8, 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 bfa563c into pmd:main Sep 8, 2026
13 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] SimplifyBooleanReturns triggers inconsistently depending on redundant parentheses in return expression

3 participants