False positive on java:S2259 with @Nullable generics parameter

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

Hello @PeerHeijnen,

Thank you for the report and the clear reproducer.

I can confirm that there are a few issues related to this rule and its current implementation. We are actively working on a better alternative. In the meantime, you can track this particular issue with this ticket.

Are you using SonarLint in connected mode or standalone? If you are using connected mode, what type of SonarQube are you connected to?