[JAVA] squid:SwitchLastCaseIsDefaultCheck - False positive?

  • versions used (SonarQube Version 6.7.4, SonarAnalyzer, java)
  • minimal code sample to reproduce:
    Test.java
 public static String test(final MyEnum enumeration) {

        String result = "";
        switch (enumeration) {
            case TAI:
                result = "toto";
                break;

            case UTC:
                result = "titi";
                break;

        }
        return result;
    }

MyEnum.java

public enum MyEnum {


    /**
     * International Atomic Time.
     * 
     */
    TAI,

    /**
     * Coordinated Universal Time.
     * 
     */
    UTC;

    public String value() {
        return name();
    }

    public static MyEnum fromValue(String v) {
        return valueOf(v);
    }

}

Sonarqube report that I should add a default case in “Test.java” even if all the constants of the enum “MyEnum” are used.

It seems to be a false positive, isn’t it ?

Hello,

An exception was added to the rule S131 to not raise an issue when all the constants of an enum are used in the case statements: https://jira.sonarsource.com/browse/SONARJAVA-1883. This is in SonarJava since 4.3

Do you reproduce the FP when using latest version of SonarJava (5.14): https://rules.sonarsource.com/java/RSPEC-131?

Thanks

Hello,

I’m using SonarJava 5.3 and unfortunately, I’m not able to update the plugin.

Thanks

Hello,

Sorry, but we are not supporting old versions of plugins, if you can not move to latest release of SonarJava, then we won’t be able to help you.

Now, note that SonarQube latest LTS is 7.9, and older version are not supported anymore.

Regards,
Michael

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