You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] SimplifyBooleanReturns should consider literal expression #3852
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:
publicbooleanfoo() {
if (true) { // should report a warning in this if-else statementreturn (true || false);
} else {
returnfalse;
}
}
Expected outcome:A warning
PMD should report a violation at line 2, but doesn't. This is a false-negative.
changed the title [-]SimplifyBooleanReturns should consider literal expression[/-][+][java] SimplifyBooleanReturns should consider literal expression[/+]on Mar 17, 2022
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?
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))
Activity
[-]SimplifyBooleanReturns should consider literal expression[/-][+][java] SimplifyBooleanReturns should consider literal expression[/+]adangel commentedon Mar 17, 2022
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?
oowekyala commentedon Mar 17, 2022
FWIW I believe SimplifyBooleanReturns, as updated in PMD 7, already handles this correctly. It doesn't pick up on
(true || false)but on thereturn false, because this means everything can be rewritten ascondition && thenBranch(ietrue && (true || false))jsotuyod commentedon Mar 26, 2024
This is effectively no longer reproducible under PMD 7.0.0
[java] Add test case for pmd#3852