S5411 FP after doing a null check

Windows Eclipse using SonarLint plugin 7.9.0.66038 for java

Rule states it is not raised for booleans that have been null checked.
As can be seen in the following code ‘value’ has been null checked but rules is still raised.

    if (value == null)
    {
        return "null";
    }

    try
    {
        type = metaData.getColumnType(column+1);
    }
    catch (SQLException e)
    {
        return value.toString();
    }

    switch(type) 
    {
        case Types.INTEGER:
        case Types.DOUBLE:
        case Types.FLOAT:
            return value.toString();
        case Types.BIT:
            return (Boolean)value ? "1" : "0";
        case Types.DATE:
            return value.toString(); // This will need some conversion.
        default:
            return "\""+value.toString()+"\"";
    }

Hey there.

Is the issue still raised with the latest version of SonarLint for Eclipse, v7.11?

Yes, it still occurs in v7.11

Is this being worked on?

Hey Kevin,

thanks for reporting!

I can confirm the issue and it turns out that this is a current limitation of the rule implementation. See these both examples:

  String foo(Boolean value) {
    if (value == null) return "";
    return value ? "1" : "0"; // Compliant
  }

vs.

  String foo(Object value) {
    if (value == null) return "";
    return (Boolean) value ? "1" : "0"; // S5411 raised
  }

The problem is that the described exception from the rule: “not raised when already null-checked”, accounts only if it is the very same expression (value), while (Boolean) value is another expression. The current implementation of the rule does not perform a more sophisticated data flow analysis considering also the subexpressions of the expression.

Therefore, (Boolean) value accounts as “not null-checked” for the rule, even though value has been checked for null.

I created a ticket for that: SONARJAVA-4516

What you could do for the moment is to try to refactor this method accordingly, or disable the rule for the method: @SuppressWarnings("squid:S5411")