From 698ce20959032590456ceda7347805aaaa3f220b Mon Sep 17 00:00:00 2001 From: Sumit Bhagwani Date: Tue, 8 Dec 2020 13:07:24 -0800 Subject: [PATCH] NullTernary shouldn't crash on JDK 14 Fixes #1982 PiperOrigin-RevId: 346393062 --- .../google/errorprone/util/ASTHelpers.java | 11 +++++++-- .../errorprone/util/RuntimeVersion.java | 5 ++++ .../bugpatterns/NullTernaryTest.java | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index 0b19bb2bd7a..caa556a902a 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -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 diff --git a/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java b/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java index 987727a0ad3..51e66774ed0 100644 --- a/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java +++ b/check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java @@ -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; diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NullTernaryTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NullTernaryTest.java index 162382e714d..55d58c55070 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NullTernaryTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NullTernaryTest.java @@ -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; @@ -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(); + } }