Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnnecessaryDefaultInEnumSwitch does not work with enhanced switch statement for cases with multiple values #4443

Closed
robinst opened this issue Jun 20, 2024 · 0 comments · Fixed by #4498

Comments

@robinst
Copy link

robinst commented Jun 20, 2024

With UnnecessaryDefaultInEnumSwitch set to ERROR, the following code:

enum Type {
    FOO, BAR, BAZ
}

public static void main(String[] args) {
    var type = Type.valueOf(args[0]);
    switch (type) {
        case FOO -> {
            System.out.println("Hi foo");
        }
        case BAR, BAZ -> {
        }
        default -> throw new AssertionError(type);
    }
}

Does not error but I would expect it to.

Note that changing the case BAR, BAZ into separate cases like this:

        case BAR -> {
        }
        case BAZ -> {
        }

Makes the check work as expected:

src/main/java/example/Hello.java:18: error: [UnnecessaryDefaultInEnumSwitch] Switch handles all enum values: the default case can be omitted to enable enforcement at compile-time that the switch statement is exhaustive.
            default -> throw new AssertionError(type);
            ^
    (see https://errorprone.info/bugpattern/UnnecessaryDefaultInEnumSwitch)

This is with bazel version 7.2.0. I'm not sure what version of error-prone that is (not super familiar with bazel).

I pushed a minimal example repo for this here: https://github.com/robinst/error-prone-unnecessary-default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant