- Java
- java:S2259
- SonarQube v9.9
- Why do you believe it’s a false-positive/false-negative?
- SonarQube is reporting A “NullPointerExcpetion” could be thrown when add() is called in the following but stuff cannot be null when add is called
public Collection<String> notNullFalsePositive(Object x) {
final boolean y = x == null;
final Collection<String> stuff = y ? new ArrayList<>() : null;
if (y) {
stuff.add("something");
}
return stuff;
}
Interestingly, it does not report a potential NullPointerException for the following simpler snippet.
public Collection<String> notNullFalsePositive(boolean y) {
final Collection<String> stuff = y ? new ArrayList<>() : null;
if (y) {
stuff.add("something");
}
return stuff;
}