Hello, I have noticed a sonar bug report, which I believe is a false-positive. Consider the following code:
public final class Test {
private static final BigDecimal IDENTITY = BigDecimal.ZERO;
private Test() {
}
private static <T> T valueOrDefault(T value, T defaultValue) {
return value == null ? defaultValue : value;
}
public static BigDecimal bigDecimalSum(Collection<BigDecimal> values) {
BigDecimal sum = IDENTITY; // when null is assigned here, there is no failure
for (BigDecimal v : values) {
if (v != null) {
sum = valueOrDefault(sum, IDENTITY).add(v); // S2259 occurs here
}
}
return sum;
}
}
Sonar reports a possible NullPointerException being thrown (I have marked the line with a //S2259 comment), however, I cannot see how this is possible. Whenever the second argument to valueOrDefault is not null, the function may never return null, and that’s the case here (IDENTITY static final field is assigned to a non-null value).
Interestingly, when I assign the sum variable on the first line of bigDecimalSum function to null, and not to IDENTITY, the sonar warning disappears.
I can reproduce this using Eclipse 20.09 and SonarLint 5.6.0.25634.