Skip to content

[java] Fix #6274: UselessParentheses treats ternary else-branch parentheses as clarifying - #6950

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/uselessparentheses-ternary-else-6274
Aug 9, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/uselessparentheses-ternary-else-6274

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

UselessParentheses no 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

UselessParenthesesRule classified the else-branch nested ternary as NEVER (reported under the default ignoreClarifying=true) but the then-branch as ALWAYS (not reported).

How

Classify the else-branch nested ternary as CLARIFYING instead of NEVER. Under the default config it is no longer reported; with ignoreClarifying=false it is still reported. One-line change.

Testing

  • New XML cases: default config asserts 0 violations on both branches; ignoreClarifying=false asserts the else-branch is reported (locking the CLARIFYING classification).
  • Verified the default-config case fails on the old code (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.
  • Updated one unit test that encoded the old NEVER behavior.

Notes

  • A residual asymmetry remains under ignoreClarifying=false (then-branch ALWAYS, else-branch CLARIFYING). Fully symmetric would require the then-branch to become CLARIFYING too — a larger behavior change, left for the maintainer to decide.

Fixes #6274

…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]>
@pmd-actions-helper

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

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 25 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 57 violations, 0 errors and 0 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-09 17:44:20+00:00 for de8cddf)

@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 9, 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 499df0a into pmd:main Aug 9, 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] UselessParentheses: False positive in ternary else expression

3 participants