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