False Positive for Rule S1479 on Enum-Based Switch in Java in SonarQube v2025.1.0

  • What language is this for? Java

  • Which rule? S1479 - “switch statements should not have too many case clauses”

  • Why do you believe it’s a false-positive/false-negative? The switch statement is directly over an enum type (OrderStatus) with 31 cases. According to the rule documentation, switch statements over enums should be ignored. However, SonarQube flags this switch as having too many cases, which seems to be a false positive.

  • Are you using

    • SonarQube Cloud? No
    • SonarQube Server / Community Build - which version? SonarQube Server v2025.1.0 (Build 102418)
    • SonarQube for IDE - which IDE/version? No
      • in connected mode with SonarQube Server / Community Build or SonarQube Cloud? Connected to SonarQube Server (Community Edition)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

public enum OrderStatus {
    PENDING, PROCESSING, ... UNDER_REVIEW // 31 values
}

public class OrderProcessor {
    public void processOrder(OrderStatus status) {
        switch (status) {
            case PENDING:
                System.out.println("Order is pending.");
                break;
            case PROCESSING:
                System.out.println("Order is being processed.");
                break;
            // ... All 31 cases without any empty case
            case UNDER_REVIEW:
                System.out.println("Order is under review.");
                break;
        }
    }
}

Hi Darshan,

  1. I can reproduce this problem when the Java analyzer fails to resolve the type OrderStatus. This is a problem in the rule implementation, so I created SONARJAVA-5341 to fix this bug.
  2. But there is a second problem, I don’t know why the OrderStatus is not properly resolved as an enum. Either OrderStatus.java is a file in your project, but its compiled form, OrderStatus.class, can not be resolved through the classpath configuration given to the Java analyzer. Or, OrderStatus.class is in a jar not present on the classpath. It may be a SonarQube for IDE configuration issue, and in this case, I don’t know how to help you fix it.

Thank you for your feedback,
Alban