Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
8270006: Switches with 'case null:' should be exhaustive
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jul 8, 2021
1 parent 8f798b8 commit 4f70759
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Expand Up @@ -664,7 +664,11 @@ public void visitSwitch(JCSwitch tree) {
ListBuffer<PendingExit> prevPendingExits = pendingExits;
pendingExits = new ListBuffer<>();
scan(tree.selector);
Set<Symbol> constants = tree.patternSwitch ? new HashSet<>() : null;
boolean exhaustiveSwitch = tree.patternSwitch ||
tree.cases.stream()
.flatMap(c -> c.labels.stream())
.anyMatch(l -> TreeInfo.isNull(l));
Set<Symbol> constants = exhaustiveSwitch ? new HashSet<>() : null;
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
alive = Liveness.ALIVE;
JCCase c = l.head;
Expand All @@ -686,7 +690,7 @@ public void visitSwitch(JCSwitch tree) {
l.tail.head.pos(),
Warnings.PossibleFallThroughIntoCase);
}
if (!tree.hasTotalPattern && tree.patternSwitch &&
if (!tree.hasTotalPattern && exhaustiveSwitch &&
!TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases) &&
(constants == null || !isExhaustive(tree.selector.type, constants))) {
log.error(tree, Errors.NotExhaustiveStatement);
Expand Down
5 changes: 5 additions & 0 deletions test/langtools/tools/javac/patterns/SwitchErrors.java
Expand Up @@ -185,4 +185,9 @@ Object guardWithMatchingExpression(Object o1, Object o2) {
default -> null;
};
}
void exhaustiveAndNull(String s) {
switch (s) {
case null: break;
}
}
}
3 changes: 2 additions & 1 deletion test/langtools/tools/javac/patterns/SwitchErrors.out
Expand Up @@ -42,6 +42,7 @@ SwitchErrors.java:91:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:97:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:104:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:164:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:189:9: compiler.err.not.exhaustive.statement
- compiler.note.preview.filename: SwitchErrors.java, DEFAULT
- compiler.note.preview.recompile
44 errors
45 errors

1 comment on commit 4f70759

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.