Remove requirement for default in Java 11 switches using enum

  • Version: 7.9.4 (build 35981)

When using Java 11 and a switch statement with enums SQ forces the adding of a default which because the switch is enum driven is unreachable. Because it is unreachable it can’t be tested and the code is consequently marked as uncovered.

Is there a current option to turn this off for this case? If not, is there a possibility of adding a way of turning of forcing defaults when you use an enum in Java 11 switches?


public enum OPERATION {CREATE, MERGE, DELETE}

public void doOperation(final OPERATION operation) {
    switch (operation) {
        case CREATE:
            System.out.println("CREATE called");
            break;
        case MERGE:
            System.out.println("MERGE called");
            break;
        case DELETE:
            System.out.println("DELETE called");
            break;
        default:
            System.out.println(
                    "SQ forces default to be added and this code is unreachable but is marked as untested code");
    }
}

doOperation(OPERATION.CREATE);

Hi Tom,

Thanks for your message.

We fixed the issue in the SQ 9.0 and updated the rules description as you can read on this page.

Regards,
Richard

1 Like

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