False Positive on squid:S2583

Versions used

  • SonarScanner 4.2.0.1873-macosx
  • Sonarqube 8.2.0.32929 Docker

Error observed
With the following code block

if (FeatureFlag.DEBUG) {
    MyLogger.d(TAG, "notifyFindContacts : "
        + "users=" + users != null ? Arrays.toString(users.toArray()) : "null"
        + ", buddies" + buddies != null ? Arrays.toString(buddies.toArray()) : "null");
}

This issue’s description says:
Change this condition so that it does not always evaluate to “true”

  1. Implies ‘users’ is not null.
"notifyFindContacts : "
        + "users=" + users
  1. Expression is always true.
"notifyFindContacts : "
                    + "users=" + users != null

However, users is nullable and I guess there is something wrong with Sonarqube while parsing the string “user=”.

+ has a higher precedence than != or ?: (see Operators). What you are actually testing is

("notifyFindContacts : " + "users=" + users) != null

What you want is probably

"notifyFindContacts : " + "users=" + (users != null ? Arrays.toString(users.toArray()) : "null")
1 Like

You are right, thank you.