A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/3786 below:

[java] SimplifyBooleanReturns should consider operator precedence · Issue #3786 · pmd/pmd · GitHub

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]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4