java:S6205 switch expressions with if need blocks

Consider the following example:

void foo(String s) {
    switch (s) {
       case "a" -> foo("bar");
       case "b" -> {
            if (s.isBlank()) foo(s);
        }
        default -> foo("baz");
    }
}

Sonar warns on the opening curly brace of the "b" case: “Remove this redundant block.” linking to rule java:S6205. However removing the curly braces results in non compiling code (“Syntax error, type annotations are illegal here”) and also still the Sonar warning is present.

SonarLint for Eclipse 7.4.0.46482
Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components)
Version: 2022-03 (4.23.0)
Build id: 20220310-1457

openjdk version “17.0.3” 2022-04-19
OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)
OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, sharing)

Hello @Madjosz

You are right, it is not possible to remove the block in this case and the analyzer should not report an issue. Ticket created to improve the situation: SONARJAVA-4243.

Thanks for taking the time to report this issue with a clear reproducer.

Best,
Quentin

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