Skip to content

[java] SimplifyBooleanReturns: False negative when if-return is wrapped inside unnecessary anonymous block #6519

@leemeii

Description

@leemeii

Affects PMD Version: 7.22.0

Rule:SimplifyBooleanReturns

Description:
The SimplifyBooleanReturns rule fails to trigger on the classic if-return boolean pattern when it is wrapped inside an unnecessary anonymous block {}.

This is a clear false negative — both patterns are semantically identical and can be safely replaced by a single return statement, yet only the direct case is flagged.

Code Sample demonstrating the issue:

public class a{
    public boolean TP(Object o) {
        if(obj == null) return false;   // TP: correctly reported
        return true;
    }
    public boolean FN(Object o) {
        {
            if(obj == null) return false;   // FN: should report but doesn't
        }
        return true;
    }
}

Expected outcome:
PMD should report SimplifyBooleanReturns for the if statement in both TP() and FN() with the message:
"This if statement can be replaced by return !{condition};"
But currently it only reports the direct case (TP), completely missing the extra block case (FN).

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions