Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8270006: Switches with 'case null:' should be exhaustive
Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed Jul 8, 2021
1 parent 8f798b8 commit 4f707591754e5e7f747d1d0a47f78f49060771c2
Showing 3 changed files with 13 additions and 3 deletions.
@@ -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;
@@ -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);
@@ -185,4 +185,9 @@ Object guardWithMatchingExpression(Object o1, Object o2) {
default -> null;
};
}
void exhaustiveAndNull(String s) {
switch (s) {
case null: break;
}
}
}
@@ -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

@openjdk-notifier openjdk-notifier bot commented on 4f70759 Jul 8, 2021

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.