diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitch.java b/core/src/main/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitch.java index 7f18b1b7482..c0bf9003e3c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitch.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitch.java @@ -18,7 +18,6 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; -import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.isSwitchDefault; import com.google.common.collect.ImmutableSet; @@ -60,7 +59,9 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) { ImmutableSet handled = cases.stream() .flatMap(c -> c.getExpressions().stream()) - .map(e -> getSymbol(e).getSimpleName().toString()) + .map(ASTHelpers::getSymbol) + .filter(x -> x != null) + .map(symbol -> symbol.getSimpleName().toString()) .collect(toImmutableSet()); Set unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled); if (unhandled.isEmpty()) { diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitchTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitchTest.java index 34461b3f7fd..312cccceadb 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitchTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitchTest.java @@ -309,4 +309,40 @@ void m(Case c) { """) .doTest(); } + + @Test + public void i4684() { + compilationHelper + .addSourceLines( + "ErrorProneBug.java", + """ + public class ErrorProneBug { + enum A { + A1, + A2, + A3 + } + + public static void main(String[] args) { + A a = null; + + switch (a) { + case null -> { + System.out.println("null"); + } + case A1 -> { + System.out.println("A1"); + } + case A2 -> { + System.out.println("A2"); + } + case A3 -> { + System.out.println("A3"); + } + } + } + } + """) + .doTest(); + } }