S2259 false positive with "transitive" non-null values

SonarLint for Eclipse 7.7 standalone, Language Java gives a false positive for S2259:

public enum S2259Enum {
  ENUM_A,
  ENUM_B;

  public String getOptionString(S2259Enum select) {
    StringBuilder res = new StringBuilder();
    for (S2259Enum typ : S2259Enum.values()) {

      if (select == typ || (typ == ENUM_A && select == null)) {
        res.append("<option value=\"")
            .append(typ.name()) // S2259 occurs here
            .append("\" selected>")
            .append(typ.toString())
            .append("</option>");
      } else {
        res.append("<option value=\"")
            .append(typ.name())
            .append("\">")
            .append(typ.toString())
            .append("</option>");
      }
    }
    return res.toString();
  }
}

The parameter typ cannot be null, because it is always an instance of the enum S2259Enum. The analyzer might think it can be null because the two OR-cases in the if-Clause imply that select might be null and therefore, typ might also be null. But: If select ist null then typ cannot be null (has to be ENUM_A) and the first OR-Clause does not evaluate to true.

1 Like

Hello @leine, thank you for reporting this False Positive. I created SONARJAVA-4554 to address it.

You may be interested to know that we’re working on a new engine for Dataflow Bug Detection that will be able to correctly handle such use cases that the current Symbolic Execution Engine for Java may miss. Keep an eye on the future releases of Sonar Qube.