Skip to content

Commit b222ea8

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Handle qualified enum elements in MissingCasesInEnumSwitch.
PiperOrigin-RevId: 697598061
1 parent 332cbfa commit b222ea8

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitch.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.collect.ImmutableSet.toImmutableSet;
2020
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
21+
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2122
import static com.google.errorprone.util.ASTHelpers.isSwitchDefault;
2223

2324
import com.google.common.collect.ImmutableSet;
@@ -27,9 +28,11 @@
2728
import com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher;
2829
import com.google.errorprone.matchers.Description;
2930
import com.google.errorprone.util.ASTHelpers;
30-
import com.sun.source.tree.IdentifierTree;
31+
import com.sun.source.tree.CaseTree;
32+
import com.sun.source.tree.ExpressionTree;
3133
import com.sun.source.tree.SwitchTree;
3234
import com.sun.tools.javac.code.Type;
35+
import java.util.List;
3336
import java.util.Set;
3437
import java.util.stream.Collectors;
3538
import javax.lang.model.element.ElementKind;
@@ -44,25 +47,26 @@ public class MissingCasesInEnumSwitch extends BugChecker implements SwitchTreeMa
4447

4548
@Override
4649
public Description matchSwitch(SwitchTree tree, VisitorState state) {
47-
Type switchType = ASTHelpers.getType(tree.getExpression());
50+
ExpressionTree expression = tree.getExpression();
51+
List<? extends CaseTree> cases = tree.getCases();
52+
Type switchType = ASTHelpers.getType(expression);
4853
if (switchType.asElement().getKind() != ElementKind.ENUM) {
4954
return Description.NO_MATCH;
5055
}
5156
// default case is present
52-
if (tree.getCases().stream().anyMatch(c -> isSwitchDefault(c))) {
57+
if (cases.stream().anyMatch(c -> isSwitchDefault(c))) {
5358
return Description.NO_MATCH;
5459
}
5560
ImmutableSet<String> handled =
56-
tree.getCases().stream()
61+
cases.stream()
5762
.flatMap(c -> c.getExpressions().stream())
58-
.filter(IdentifierTree.class::isInstance)
59-
.map(e -> ((IdentifierTree) e).getName().toString())
63+
.map(e -> getSymbol(e).getSimpleName().toString())
6064
.collect(toImmutableSet());
6165
Set<String> unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled);
6266
if (unhandled.isEmpty()) {
6367
return Description.NO_MATCH;
6468
}
65-
return buildDescription(tree).setMessage(buildMessage(unhandled)).build();
69+
return buildDescription(expression).setMessage(buildMessage(unhandled)).build();
6670
}
6771

6872
/**

core/src/test/java/com/google/errorprone/bugpatterns/MissingCasesInEnumSwitchTest.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,36 @@ void m(Case c) {
5656
.doTest();
5757
}
5858

59+
@Test
60+
public void exhaustive_allowsQualifying() {
61+
assume().that(Runtime.version().feature()).isAtLeast(21);
62+
compilationHelper
63+
.addSourceLines(
64+
"Test.java",
65+
"""
66+
class Test {
67+
enum Case {
68+
ONE,
69+
TWO,
70+
THREE
71+
}
72+
73+
void m(Case c) {
74+
switch (c) {
75+
case Case.ONE:
76+
case Case.TWO:
77+
case Case.THREE:
78+
System.err.println("found it!");
79+
break;
80+
}
81+
}
82+
}
83+
""")
84+
.doTest();
85+
}
86+
5987
@Test
6088
public void exhaustive_multipleCaseExpressions() {
61-
assume().that(Runtime.version().feature()).isAtLeast(14);
6289
compilationHelper
6390
.addSourceLines(
6491
"Test.java",
@@ -209,7 +236,6 @@ void m(Case e) {
209236

210237
@Test
211238
public void nonExhaustive_arrowStatement() {
212-
assume().that(Runtime.version().feature()).isAtLeast(14);
213239
compilationHelper
214240
.addSourceLines(
215241
"Test.java",
@@ -235,7 +261,6 @@ void m(Case c) {
235261

236262
@Test
237263
public void nonExhaustive_multi() {
238-
assume().that(Runtime.version().feature()).isAtLeast(14);
239264
compilationHelper
240265
.addSourceLines(
241266
"Test.java",
@@ -261,7 +286,6 @@ void m(Case c) {
261286

262287
@Test
263288
public void nonExhaustive_multiArrow() {
264-
assume().that(Runtime.version().feature()).isAtLeast(14);
265289
compilationHelper
266290
.addSourceLines(
267291
"Test.java",

0 commit comments

Comments
 (0)