Rule S2259 reports one false positive issue about potential NullPointerExceptions for the the following code:
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiFunction;
public class S2259NullableGenericTypeParameters {
public static <LE, RE, PE, L extends Collection<LE>, R extends Collection<RE>> List<PE> s2259(
final @NonNull L left,
final @NonNull R right,
final @NonNull BiFunction<@Nullable LE, @Nullable RE, PE> combiner
) {
final Iterator<LE> leftIterator = left.iterator();
final Iterator<RE> rightIterator = right.iterator();
final List<PE> results = new ArrayList<>();
results.add(combiner.apply(leftIterator.next(), rightIterator.next())); // SonarQube: A "NullPointerException" could be thrown; "combiner" is nullable here.
return results;
}
}
- Null pointers should not be dereferenced java:S2259
- SonarQube Community Build v24.12.0.100206
- Java Code
Since combiner is annotated with @NonNull, it will never be null. Based on this assumption, I removed the @Nullable annotations from the two generic type parameters LE and RE, and SonarQube no longer reports any issues.
Marino