We get a “Nullpointers should not be dereferenced” (S2259) message on the following example code:
import java.util.*;
import org.jetbrains.annotations.*;
public class RuleJavaS2259
{
public static void main( final String[] args ) {
example( List.of() );
}
static void example( final @NotNull List<@Nullable Object> list ) {
System.out.println( list.size() );
}
}
In the above code list
cannot be null, but elements inside the list can, this is specified by the annotations.
It appears that the @Nullable
annotation is considered to apply to list
instead of the generic parameter (that indicates that the list may contain nulls).
If I remove all annotations, the rule is not triggered. If I remove only the @NotNull
, the rule is still (falsely) triggered.
Environment:
- Java: JetBrains s.r.o. 21.0.3
- OS: Mac OS X x86_64
- IDE: IntelliJ IDEA 2024.3
- SonarLint: 10.13.1.80133