GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |
GeezerVison is showing github.com — [java] Fix #6274: UselessParentheses treats ternary else-branch parentheses as clarifying by fudianchn · Pull Request #6950 · pmd/pmd Conversation
Contributor
What
WhyBoth 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
HowClassify the else-branch nested ternary as Testing
Notes
Fixes #6274
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions…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
|
There was an error while loading. Please reload this page.
|
Compared to main: (comment created at 2026-08-09 17:44:20+00:00 for de8cddf) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
[java] UselessParentheses: False positive in ternary else expression
There was an error while loading. Please reload this page.