Sonar - Conditions must not unconditionally evaluate to "TRUE" or "FALSE"

Hi i wrote below code to check to indicate equal or not

 private  boolean equalTypeLists(List<Type> one, List<Type> two){
    if (one == null && two == null){
        return true;
    }
    if((one == null && two != null)
            || one != null && two == null
            || one.size() != two.size()){
        return false;
    }
    one = new ArrayList<>(one);
    two = new ArrayList<>(two);

    Collections.sort(one, new Comparator<Type>() {
        @Override
        public int compare(Type o1, Type o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });
    Collections.sort(two, new Comparator<Type>() {
        @Override
        public int compare(Type o1, Type o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });

    return checkForTwoEqualTypeLists(one,two);
}

      

But Sonar gives Blocker message. "Conditions must not unconditionally evaluate to" TRUE "or" FALSE "for the next line of code

if((one == null && two != null)
        || one != null && two == null
        || one.size() != two.size()){
    return false;
}

      

Please, could you help me to solve the above scenario.

+3


source to share


1 answer


From sonar site :

Conditions should not unconditionally evaluate to the value "TRUE" or "FALSE"

Conditional statements using a condition that cannot be anything other than FALSE will cause blocks of code to fail. If the condition cannot evaluate to anything other than TRUE, the conditional expression is completely redundant and makes the code less readable.

It is highly likely that the code does not match the programmer's intent. Either the condition must be removed, or it must be updated so that it does not always evaluate to TRUE or FALSE.

The last sentence helps to identify the fix:

Either the condition must be removed, or it must be updated so that it does not always evaluate to TRUE or FALSE.

In your case, sonar conditional statements as violation of the rules should not be updated, but deleted, because even if they are true, they are also redundant.




In this code, if the operator if

is equal false

, it means that at least one

or two

(and maybe even both) is not null

:

if (one == null && two == null){
    return true;
}

      

But in the second expression, if

you repeat this test:

if((one == null && two != null)
        || one != null && two == null
        || one.size() != two.size()){
    return false;
}

      

Checks != null

here: (one == null && two != null)

and are one != null && two == null

not required as we know that when we reach this code, if one of them is ( one

or two

) is equal null

, the other one is necessarily not null

(output of the first statement if

).

So this should be enough:

if (one == null && two == null){
    return true;
}

if(one == null || two == null || one.size() != two.size()){
    return false;
}

      

+2


source







All Articles