Pmd: [java] SimplifiedTernary: Incorrect ternary operation can be simplified.

Created on 2 Feb 2017  路  4Comments  路  Source: pmd/pmd

Rule Set: basic.html#SimplifiedTernary

Description: I have a special case where to save money in my database, I return null if a boolean value is false in a method of return type Boolean.

Code Sample demonstrating the issue:

@Nullable
public Boolean getHasCustomName() {
    return mHasCustomName ? true : null; // From my understanding of Java, this cannot be simplified.
}

Running PMD through: Gradle

bug

Most helpful comment

@sschuberth actually that is not a false positive. The rule is right, you could rewrite that without the conditional as:

setToogle(varOfBooleanClass != null && varOfBooleanClass);

All 4 comments

Thanks for reporting. This bug will be fixed with PMD 5.4.5, 5.5.4, and later.

@adangel Has this really been fixed for 5.5.4? Because for me 5.5.4 still has this issue, although with a slightly different piece of code:

setToogle(varOfBooleanClass != null ? varOfBooleanClass : false);

@sschuberth actually that is not a false positive. The rule is right, you could rewrite that without the conditional as:

setToogle(varOfBooleanClass != null && varOfBooleanClass);

@jsotuyod Doh, you're right, thanks for opening my eyes!

Was this page helpful?
0 / 5 - 0 ratings