java:S2326 false positive with sealed interface type parameters

If your question is about SonarLint in the IntelliJ Platform, VS Code, Visual Studio, or Eclipse, please post it in that sub-category.

Otherwise, please provide:

  • Operating system: macOS
  • IDE name and flavor/env: IntelliJ IDEA 2023.3.2
  • SonarLint Plugin version: 10.2.0.77238
  • Java Version: Amazon Corretto JDK 21

And a thorough description of the problem / question:

If you define a sealed interface with type parameters that are used by implementers of the interface as in the following Either monad example, SonarLint incorrectly flags the type parameters on the interface as not used.

public sealed interface Either<L,R> permits Right, Left {
  record Right<L,R>(R value) implements Either<L,R> {}
  record Left<L,R>(L value) implements Either<L,R> {}
}

Hello @cssprs,

Thanks for your report.

This is indeed a FPs. However, it’s a tricky one as in order to check it properly we should know all the implementations of it. Or we can simply ignore all interfaces/extendable classes, which might lead to many FNs.

I’ve created a ticket to fix the issue and we will discuss possible approaches with the team:

https://sonarsource.atlassian.net/browse/SONARJAVA-4751

Meanwhile, since this is not a very common practice, do you think it makes sense to have a @SuppressWarning() annotation? This will tell developers that this FP happens due to the current limitation of static analysis and the parameter is actually used.

Just a side note, looks like IntelliJ Idea inspection has the same issue (at least on my machine).

Best,
Margarita