[java:s6916] FP when if inside multi case

Sonar Qube: Community Build v25.7.0.110598
SonarLint for Eclipse: 11.14.0.83019

Sonar Qube/Lint produces an FP for rule java:s6916 in this code.

Integer i = …;

switch ( i )
{
case 1, 2, 3 → {
if ( someCondition )
System.out.println( “foo” );
}
default → System.out.println( “” );
}

It suggests to use a pattern match guard. The quick fix produces this:

switch ( i )
{
case 1, 2, 3 when someCondition → {
System.out.println( “foo” );
}

**default** -> System.***out***.println( "" );

}

Obviously this is uncompileable. Pattern match guards can only be used as a stand alone case in a switch and cannot be combined with other case labels.

Hey there.

Thanks for the report! I think we’re already tracking this at SONARJAVA-4962.

Yes, looks like that. Thanks.

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