False positive from S3824

Hello,

  • What language is this for? Java
  • Which rule? S3824
  • Why do you believe it’s a false-positive/false-negative? No exception would be thrown if we’d use computeIfAbsent.
  • Are you using
    • SonarCloud? No
    • SonarQube - which version? Yes, 9.9.1
    • SonarLint - which IDE/version? Yes, 7.13 in Eclipse
      • in connected mode with SonarQube or SonarCloud? yes
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
class S3824FalsePositive {

    void addOrThrow(java.util.Map<String, String> map, String key, String value) {
        if (value != null) {
            if (map.containsKey(key)) { // <- Replace this "Map.containsKey()" with a call to "Map.computeIfAbsent()".
                throw new IllegalArgumentException("map already contains key!");
            }
            map.put(key, value);
        }
    }
}

Also note that if we remove the if (value != null) check, then no issue is reported.

Thanks
Vivien

Hi Vivien,

Confirmed! I created a ticket here to fix the issue.

Thanks for your report!

Marco

Hi @Marco_Kaufmann,

I also ran into this false positive. Is there any news on this issue?

Thanks
Luca

Hello @lpieprzyk,

The linked ticket is public and as you can see it has not made it out of the backlog.

Per our FAQ, we ask that you not resurrect threads that have been dormant for more than 2 months. Things change quickly and old information may no longer be relevant.

If you’re still seeing this issue, please open a new thread with all the relevant details (SonarQube version, scanner version, a code snippet that reproduces the issue, etc.) and we’ll take a look from there.

Cheers,

Stevan