Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube - Java rule "S128" - Why the rule complains about the fact that a break statement is missing when this is obviously not necessary?

Tags:

java

sonarqube

I can't really figure out why Sonar keeps complaining about the fact that I "don't have a break statement" even if it's not needed..

My switch:

    public static String lookupVoyageId(String referenceNumber, String sender) {
    switch (sender) {
        case "400_HGENT":
        case "200_HAPEN":
        case "500_HOOST":
            Preconditions.checkArgument(referenceNumber.contains("-"));
            return referenceNumber.split("-")[0];
        case "600_HZEEB":
            Preconditions.checkArgument(referenceNumber.length() >= 6);
            return referenceNumber.substring(0, 6);
        case "800_BVL":
            throw new TransferException("This reference number for IBIS isn't according to the requirements. Can't implement it yet.");
        case "MCCD":
            throw new TransferException("This reference number for MCCD isn't according to the requirements. Can't implement it yet.");
        default:
            throw new TransferException("The sender (" + sender + ") couldn't be identified.");
    }
}

and sonar keeps giving me the critical: "A switch statement does not contain a break"

Why is this? I don't need any breaks in this switch?

I know it might be a specific case, but I can't find anything on the web.

like image 494
GregD Avatar asked Jun 30 '15 10:06

GregD


1 Answers

If you can't decrease the number of switch case or can't refactor the code you can suppress the warning with

 @SuppressWarnings({"squid:S128", "squid:S1479"}

Sample usage here

like image 109
Melad Avatar answered Nov 01 '22 07:11

Melad