- SonarQube Community Build v25.10.0.114319
- Java Code
- Rule S2259 Null pointers should not be dereferenced
S2259 reports a false positive issue about a potential NullPointerException for the the following Java code:
package org.example;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@NullMarked
public class S2259MapComputeIfAbsent {
public void m() {
Map<String, List<@Nullable String>> map = new HashMap<>();
map.computeIfAbsent("foo", k -> new ArrayList<>()).add("bar");
}
}
The issue is described as: A “NullPointerException” could be thrown; “computeIfAbsent()” can return null.
But computeIfAbsent never returns null here. It returns NonNull Lists (compare JSpecify NullMarked class-level annotation). The issue is only reported if elements(!) of the List are annotated as being Nullable (List<@Nullable String>) but that doesn’t matter when dereferencing the result of computeIfAbsent.
I’ve attached the above source as minimal maven project:
test.zip (1.9 KB)
I’ve read in the SonarQube release note that JSpecify annotations are supported. Is this really the case? There seem to be a couple of open issues?