Skip to content

[java] SimplifyBooleanReturns should consider operator precedence #3786

@oowekyala

Description

@oowekyala

Affects PMD Version: 7.0.0

Rule: https://pmd.github.io/latest/pmd_rules_java_design.html#simplifybooleanreturns

Description: SimplifyBooleanReturns suggests a code transformation that makes the code harder to read. This happens when transforming an if/else statement to single-return form requires introducing parentheses because of operator precedence. I think the rule should only report if the conversion would not require introducing parentheses.

Code Sample demonstrating the issue:

private boolean isFactoryMethod(ASTMethodCall expr) {
        if (expr.getQualifier() != null) { // nopmd
            return typeEndsWith(expr.getQualifier(), "Factory")
                || nameEndsWith(expr.getQualifier());
        }
        return false;
}

when converted this would be

    private boolean isFactoryMethod(ASTMethodCall expr) {
        return expr.getQualifier() != null && (typeEndsWith(expr.getQualifier(), "Factory")
            || nameEndsWith(expr.getQualifier()));
    }

which is ugly. Using an if-else statement allows avoiding parentheses.

see https://github.com/pmd/pmd/runs/5209308795?check_suite_focus=true

Expected outcome: no violation. That's a false positive.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions