Skip to content

[java] SimplifyBooleanReturns triggers inconsistently depending on redundant parentheses in return expression #6694

Closed

Description

@Chordrain

Affects PMD Version: 7.24.0

Rule: SimplifyBooleanReturns — https://docs.pmd-code.org/latest/pmd_rules_java_design.html#simplifybooleanreturns

Description:

SimplifyBooleanReturns is sensitive to purely syntactic parenthesization of the returned expression. Two semantically identical methods — one whose return expression is wrapped in a redundant outer pair of parentheses, the other not — are treated differently: the parenthesized form is flagged, the bare form is not. Since the rule targets the structural pattern if (c) return X; return false; (which should simplify to return c && X;), it should either flag both forms or neither. The discrepancy looks like the rule's AST matcher does not unwrap a ParenthesizedExpression node before checking the return-value shape, causing a false-negative on the unparenthesized form (or, equivalently, unstable behavior across trivially equivalent code).

Code Sample demonstrating the issue:

public class FactoryChecker {

    // BEFORE — NOT flagged by SimplifyBooleanReturns
    public boolean isFactoryMethodA(String qualifier, String name) {
        if (qualifier != null) {
            return typeEndsWith(qualifier, "Factory")
                || nameEndsWith(name);
        }
        return false;
    }

    // AFTER — flagged by SimplifyBooleanReturns (line of the `if`)
    public boolean isFactoryMethodB(String qualifier, String name) {
        if (qualifier != null) {
            return (typeEndsWith(qualifier, "Factory")
                || nameEndsWith(name));
        }
        return false;
    }

    private boolean typeEndsWith(String q, String suffix) { return q != null && q.endsWith(suffix); }
    private boolean nameEndsWith(String n) { return n != null && n.endsWith("Factory"); }
}

Expected outcome: Both methods (which are semantically identical) should receive the same verdict — either both are reported as simplifiable to return qualifier != null && (typeEndsWith(...) || nameEndsWith(...));, or neither is.

Actual outcome: Only isFactoryMethodB (with the redundant outer parentheses) is reported; isFactoryMethodA is silently accepted.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on May 18, 2026
  2. added this to the 7.28.0 milestone on Sep 8, 2026
  3. added a commit that references this issue on Sep 8, 2026
    bfa563c
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