java:S1301 should not trigger on grouped case clauses in JDK 14+

Similar to swift:S1301.

Java 14 introduced grouping multiple case clauses with comma-separated labels (java:S6208). When the switch ends up with only one or two cases, java:S1301 triggers and suggests to convert this switch to an if statement, which is obviously not the optimal solution in terms of readability.

Code example:

switch (variable) {
  case 0, 1:
    doSomething();
    break;
  default:
    doSomethingElse();
    break;
}

Sonar Lint for Eclipse
7.8.0.63129

Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components)
Version: 2022-12 (4.26.0)
Build id: 20221201-1913

openjdk version “17.0.6” 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)

2 Likes

Hello Madjosz,

Thanks for the report, good catch. Indeed, this rule was not updated to take into account the new syntax introduced with Java 14. I’ve created this ticket to address the issue.

1 Like

Thank you for creating the issue. Just a little remark:
In your code example you are mixing switch expression (yield) with switch statement (colon after case) syntax.

1 Like

Good catch, I should pay more attention when copy+pasting!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.