Skip to content

UnnecessaryDefaultInEnumSwitch provides illegal suggestion for arrow rules in switch statements #5045

Closed
@PhilippWendler

Description

@PhilippWendler

In the following example, the check UnnecessaryDefaultInEnumSwitch suggests to remove the default clause with the reason "Switch handles all enum values: the default case can be omitted to enable enforcement at compile-time that the switch statement is exhaustive." But the code would not compile then:

public class Test {

  enum TestEnum {
    FOO,
    BAR;
  }

  void unnecessaryDefault() {
    String x;

    switch (TestEnum.FOO) {
      case FOO -> x = "FOO";
      case BAR -> x = "BAR";
      default -> throw new AssertionError();
    }

    System.out.println(x);
  }
}

It seems the reason is the incomplete handling of arrow rules from e5d52dd in

List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
if (defaultStatements == null) {
// TODO(b/177258673): provide fixes for `case -> ...`
return buildDescription(defaultCase).setMessage(DESCRIPTION_REMOVED_DEFAULT).build();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions