Skip to content

Commit

Permalink
Fix an NPE on case null in MissingCasesInEnumSwitch
Browse files Browse the repository at this point in the history
Fixes #4684

PiperOrigin-RevId: 698055266
  • Loading branch information
cushon authored and Error Prone Team committed Nov 19, 2024
1 parent e840c2e commit 150334e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +59,9 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
ImmutableSet<String> 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<String> unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled);
if (unhandled.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,41 @@ void m(Case c) {
""")
.doTest();
}

@Test
public void i4684() {
assume().that(Runtime.version().feature()).isAtLeast(21);
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();
}
}

0 comments on commit 150334e

Please sign in to comment.