Repository navigation
[java] Fix #6274: UselessParentheses treats ternary else-branch parentheses as clarifying - #6950
Merged
UncleOwen merged 3 commits intoAug 9, 2026
Conversation
…rentheses as clarifying Previously, parentheses around a nested ternary in the else-branch of an outer ternary (`a ? b : (c ? d : e)`) were reported as useless, while the symmetric form in the then-branch (`a ? (b ? c : d) : e`) was accepted. This inconsistency was a false positive reported in pmd#6274. Root cause: in UselessParenthesesRule#needsParentheses, the else-branch case (inner.getIndexInParent() == 2) returned NEVER, whereas the then-branch returned ALWAYS. The parentheses are technically redundant in both positions (Java ternary is right-associative), so they should be treated as clarifying rather than useless. Fix: return CLARIFYING for the else-branch. With the default `ignoreClarifying=true` the parentheses are no longer reported, making then- and else-branches consistent. With `ignoreClarifying=false` they are still reported, matching the CLARIFYING classification of other precedence-clarifying parentheses. Endorsed by oowekyala in the issue: "maybe we could consider the parentheses in the next to last line to be clarifying too. This would be consistent with how we treat the last line." Alternative direction (not implemented; left for maintainer decision): the then-branch is currently classified as ALWAYS (necessary) even though its parentheses are also technically redundant. Making the then-branch CLARIFYING as well would make the two branches fully symmetric under `ignoreClarifying=false` too. This is a behavioral change for a separate decision, so this commit only fixes the reported false positive on the else-branch. Signed-off-by: 付典 <[email protected]>
zbynek
approved these changes
Aug 7, 2026
Contributor
|
Compared to main: (comment created at 2026-08-09 17:44:20+00:00 for de8cddf) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
UselessParenthesesno longer flags parentheses around a ternary expression in the else-branch of an enclosing ternary (a ? b : (c ? d : e)), making it consistent with the then-branch, which was already accepted.Why
Both parentheses are technically redundant but act as clarifiers; flagging only the else-branch was an inconsistency. This matches @oowekyala's suggestion on the issue: "maybe we could consider the parentheses in the next to last line to be clarifying too. This would be consistent with how we treat the last line."
Root cause
UselessParenthesesRuleclassified the else-branch nested ternary asNEVER(reported under the defaultignoreClarifying=true) but the then-branch asALWAYS(not reported).How
Classify the else-branch nested ternary as
CLARIFYINGinstead ofNEVER. Under the default config it is no longer reported; withignoreClarifying=falseit is still reported. One-line change.Testing
ignoreClarifying=falseasserts the else-branch is reported (locking theCLARIFYINGclassification).Useless parentheses around 'baz ? bam : boo') and passes after the fix../mvnw -pl pmd-java -am test -Dtest=UselessParenthesesTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false→Tests run: 60, Failures: 0, Errors: 0.NEVERbehavior.Notes
ignoreClarifying=false(then-branchALWAYS, else-branchCLARIFYING). Fully symmetric would require the then-branch to becomeCLARIFYINGtoo — a larger behavior change, left for the maintainer to decide.Fixes #6274