java:S131 fails to handle exhaustive switch statements over sealed classes

  • Language: java
  • Rule: java:S131
  • Running SonarLint under eclipse 4.30 (2023-12)

In the snippet below, the rule does not consider the switch statement exhaustive (JLS § disagrees), raises a warning and asks for a default case.
Considering enums are already handled exceptionally (permitting switches without default cases when all constants are given; see RSPEC-2590), this is a false positive and should be handled separately as well.

public class Outer {
	private abstract sealed class Foo { }
	private final class Bar extends Foo { }
	private final class Baz extends Foo { }
	public static void method(final Foo foo) {
		switch(foo) {
		case final Bar bar -> { }
		case final Baz baz -> { }

(This snippet also raises java:S1481 asking to (wrongly) remove the definitions of bar and baz, and java:S1871 asking to merge the two cases (also wrong), but those are separate issues)

Hi @ivaniesta14 ,

Thanks for the report. Indeed, this is a false positive resulting from an implementation that did not consider Java 21 language features.

Good news though, we have already fixed this upstream, so you should see this problem disappear in upcoming releases.