GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #6274: UselessParentheses treats ternary else-branch parentheses as clarifying by fudianchn · Pull Request #6950 · pmd/pmd

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 6, 2026

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@fudianchn
[java] Fix pmd#6274: UselessParentheses treats ternary else-branch pa…
7f04e2e
…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
zbynek approved these changes Aug 7, 2026
View reviewed changes
@pmd-actions-helper

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

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen added 2 commits August 9, 2026 19:25
@UncleOwen
Merge branch 'refs/heads/main' into fork/fudianchn/fix/uselessparenth…
78e95b1
…eses-ternary-else-6274
@UncleOwen
[docs] sort release notes by #id
de8cddf
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 9, 2026
UncleOwen
UncleOwen approved these changes Aug 9, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit 499df0a into pmd:main Aug 9, 2026
11 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zbynek zbynek zbynek approved these changes
@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.27.0

Development

Successfully merging this pull request may close these issues.

[java] UselessParentheses: False positive in ternary else expression

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@fudianchn @zbynek @UncleOwen
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.