Branch: refs/heads/main
  Home:   https://github.com/pmd/pmd
  Commit: 499df0a37564ac2000873c274ebb64d3af65d073
      https://github.com/pmd/pmd/commit/499df0a37564ac2000873c274ebb64d3af65d073
  Author: fudian <[email protected]>
  Date:   2026-08-09 (Sun, 09 Aug 2026)

  Changed paths:
    M docs/pages/release_notes.md
    M 
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UselessParenthesesRule.java
    M 
pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/UselessParenthesesTest.java
    M 
pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UselessParentheses.xml

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

* [java] Fix #6274: UselessParentheses treats ternary else-branch parentheses 
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 #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]>

* [docs] sort release notes by #id

---------

Signed-off-by: 付典 <[email protected]>
Co-authored-by: Sören Glimm <[email protected]>



To unsubscribe from these emails, change your notification settings at 
https://github.com/pmd/pmd/settings/notifications


_______________________________________________
Pmd-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pmd-commits

Reply via email to