Repository navigation
[java] Fix #6694: Trigger SimplifyBooleanReturns when expressions require parentheses - #6922
Conversation
…does not prevent rule trigger
kdelay
left a comment
There was a problem hiding this comment.
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:
-
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 theif (c) { ... } else { ... }shape, so they reachgetElseExprthroughnode.getElseBranch(). The report is about theif (c) return X; return false;shape, which takes thegetNextSibling()fallback instead. It is the same code path afterwards, but the reported form is currently untested. -
docs/pages/release_notes.mdhas no entry for #6694. Recent rule fixes add one under### 🐛️ Fixed Issues/javain 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.
|
Thank you for your suggestions kdelay, they are now implemented. |
|
Compared to main: (comment created at 2026-08-31 17:03:24+00:00 for 51ed211) |
|
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? |
|
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. |
|
It now does not trigger on the case which has parentheses which cannot be removed. |
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?
./mvnw clean verifypasses (checked automatically by github actions)