Skip to content

[java] UselessParentheses: False positive in ternary else expression #6274

Closed

Description

@Vampire

Affects PMD Version: 7.18.0

Rule: UselessParentheses

Description:

Given the below code sample, both of the first two bae lines are hard to read and need some operator in-head parsing to see the outcome.

The second two bae lines are imho greatly preferred to increase readability.

With ignoreClarifying enabled, the last line is indeed accepted, but the next to last line is complained about to have useless parentheses.

If that is a measure for you, IntelliJ also makes the second two bae lines out of the first two bae lines if you ask it to add clarifying parentheses.

Code Sample demonstrating the issue:

boolean foo, bar, baz, bam, boo, baa;
foo = bar = baz = bam = boo = new Random().nextBoolean();

baa = foo ? bar : baz ? bam : boo;
baa = foo ? bar ? baz : bam : boo;

baa = foo ? bar : (baz ? bam : boo);
baa = foo ? (bar ? baz : bam) : boo;

Expected outcome:

PMD reports a violation at line 7 but that's wrong. That's a false positive.

Running PMD through: Gradle

Activity

  1. added
    a:false-positive
    on Nov 26, 2025
  2. oowekyala commented on Nov 27, 2025

    @oowekyala
    Member

    Mm yes 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.

  3. changed the title [-][java] `UselessParentheses` in ternary else expression[/-] [+][java] UselessParentheses: False positive in ternary else expression[/+] on Jan 16, 2026
  4. added this to the 7.27.0 milestone on Aug 9, 2026
  5. added a commit that references this issue on Aug 9, 2026
    499df0a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions