Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullTernary shouldn't crash on JDK 14 #1988

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1565,8 +1565,15 @@ public Type visitAnnotation(AnnotationTree tree, Void unused) {

@Override
public Type visitCase(CaseTree tree, Void unused) {
SwitchTree switchTree = (SwitchTree) parent.getParentPath().getLeaf();
return getType(switchTree.getExpression());
Tree t = parent.getParentPath().getLeaf();
// JDK 12+, t can be SwitchExpressionTree
if (t instanceof SwitchTree) {
SwitchTree switchTree = (SwitchTree) t;
return getType(switchTree.getExpression());
}
// TODO(bhagwani): When the ErrorProne project switches to JDK 12, we should check
// for SwitchExpressionTree.
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public static boolean isAtLeast13() {
return MAJOR >= 13;
}

/** Returns true if the current runtime is JDK 14 or newer. */
public static boolean isAtLeast14() {
return MAJOR >= 14;
}

/** Returns true if the current runtime is JDK 15 or newer. */
public static boolean isAtLeast15() {
return MAJOR >= 15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -121,4 +124,25 @@ public void conditionalInCondition() {
"}")
.doTest();
}

@Test
public void expressionSwitch_doesNotCrash() {
assumeTrue(RuntimeVersion.isAtLeast14());
testHelper
.addSourceLines(
"Test.java",
"class Test {",
" static String doStuff(SomeEnum enumVar) {",
" return switch (enumVar) {",
" case A -> enumVar.name() != null ? \"AAA\" : null;",
" default -> null;",
" };",
" }",
" ",
" static enum SomeEnum {",
" A, B",
" }",
"}")
.doTest();
}
}