Skip to content

[java] SimplifyBooleanReturns should consider literal expression #3852

Closed

Description

Affects PMD Version:6.42.0

Rule:SimplifyBooleanReturns

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_design.html#simplifybooleanreturns

Description:
Hi, I found a false negative about the rule SimplifyBooleanReturns. Please read the following code example. PMD should have reported a warning at line 3, but it doesn't. However, I think this if-else statement can be simplified. Hence, I think this is a false negative.

Code Sample demonstrating the issue:

public boolean foo() {
    if (true) {  // should report a warning in this if-else statement
        return (true || false);  
    } else {
        return false;
    }
}

Expected outcome:A warning

PMD should report a violation at line 2, but doesn't. This is a false-negative.

Running PMD through: [Maven]

Activity

  1. ghost added
    a:false-negative
    on Mar 17, 2022
  2. changed the title [-]SimplifyBooleanReturns should consider literal expression[/-] [+][java] SimplifyBooleanReturns should consider literal expression[/+] on Mar 17, 2022
  3. adangel commented on Mar 17, 2022

    @adangel
    Member

    The code sample looks very artificial - not sure, if PMD should react on this.
    I agree, that the function can be simplified with just "return true".
    The if condition should be reported by UnconditionalIfStatement.

    The rule SimplifyBooleanReturns could be improved maybe to consider "true || false" as "true", so that it recognizes, that we have a if condition, that return true and else false and can be simplified. But I don't see that it's worth the effort to add this, because who writes "true || false"? This might be a left over from a debug session ("test code") and maybe should be reported on its own?

  4. oowekyala commented on Mar 17, 2022

    @oowekyala
    Member

    FWIW I believe SimplifyBooleanReturns, as updated in PMD 7, already handles this correctly. It doesn't pick up on (true || false) but on the return false, because this means everything can be rewritten as condition && thenBranch (ie true && (true || false))

  5. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  6. removed
    needs:pmd7-revalidation
    on Mar 26, 2024
  7. added this to the 7.0.0 milestone on Mar 26, 2024
  8. jsotuyod commented on Mar 26, 2024

    @jsotuyod
    Member

    This is effectively no longer reproducible under PMD 7.0.0

  9. added a commit that references this issue on Oct 31, 2024
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-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions