SonarQube for IntelliJ plugin version: 10.22.0.81244
IntelliJ version: IntelliJ IDEA 2024.3.5
Programming language you’re coding in: Java
Is connected mode used: No
SonarQube is incorrectly recognizing a rule java:S2259 in my code, because it does not know that ObjectUtils.isEmpty(mockObject) checks whether the ‘mockObject’ is null. It thinks that mockObject could still be null, even after the ObjectUtils.isEmpty check.
I’ve narrowed down the problem. To my understanding, SonarQube only raises this rule incorrectly for a “Nullable” function’s return value. Here’s a code snippet:
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.lang.Nullable;
import java.util.HashMap;
import java.util.Map;
public class IsEmptyNullCheck {
private final TestCache cache;
public IsEmptyNullCheck() {
this.cache = new TestCache();
}
public void codeSnippet() {
String a = cache.get("a");
if (ObjectUtils.isEmpty(a)) {
System.out.println("a is empty or null");
} else {
System.out.println("a is " + a.toUpperCase()); // Issue incorrectly raised at this line
}
}
static class TestCache {
private final Map<String, String> map;
TestCache() {
this.map = new HashMap<>();
this.map.put("a", "b");
}
// Issue only arises with this annotation
@Nullable
public String get(String key) {
return map.get(key);
}
}
}