Skip to content

Commit

Permalink
Fix a crash in NestedInstanceOfConditions.
Browse files Browse the repository at this point in the history
Fixes external #4349

PiperOrigin-RevId: 633567893
  • Loading branch information
graememorgan authored and Error Prone Team committed May 14, 2024
1 parent 2a06dd3 commit 3cfb0d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public boolean matches(Tree tree, VisitorState state) {

Types types = state.getTypes();

// Pattern matching instanceofs can have null type.
if (instanceOfTree.getType() == null) {
return false;
}

boolean isCastable =
types.isCastable(
types.erasure(ASTHelpers.getType(instanceOfTree.getType())),
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 com.google.common.truth.TruthJUnit.assume;

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 All @@ -40,4 +43,21 @@ public void positiveCase() {
public void negativeCase() {
compilationHelper.addSourceFile("NestedInstanceOfConditionsNegativeCases.java").doTest();
}

@Test
public void patternMatchingInstanceof() {
assume().that(RuntimeVersion.isAtLeast21()).isTrue();
compilationHelper
.addSourceLines(
"Test.java",
"public class Test {",
" record Struct(Object a) {}",
" public void test(Object x, Object y) {",
" if (x instanceof Struct(Integer a1)) {",
" if (y instanceof Struct(Integer a2)) {}",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 3cfb0d7

Please sign in to comment.