Skip to content

Commit

Permalink
Handle multiple labels in UnnecessaryDefaultInEnumSwitch
Browse files Browse the repository at this point in the history
Fixes #4443

PiperOrigin-RevId: 655286660
  • Loading branch information
cushon authored and Error Prone Team committed Jul 23, 2024
1 parent 7b58ec9 commit a01c164
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private static boolean trivialDefault(List<? extends StatementTree> defaultState
private static SetView<String> unhandledCases(SwitchTree tree, TypeSymbol switchType) {
ImmutableSet<String> handledCases =
tree.getCases().stream()
.map(CaseTree::getExpression)
.flatMap(ASTHelpers::getCaseExpressions)
.filter(IdentifierTree.class::isInstance)
.map(p -> ((IdentifierTree) p).getName().toString())
.collect(toImmutableSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,30 @@ public void unrecognizedCaseKindRule() {
"}")
.doTest();
}

@Test
public void multipleLabels() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"class Test {",
" 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 -> {",
" }",
" // BUG: Diagnostic contains: UnnecessaryDefaultInEnumSwitch",
" default -> throw new AssertionError(type);",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit a01c164

Please sign in to comment.