diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java b/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java index 7cf58e7e22a..f58f3ea7bbb 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java @@ -101,7 +101,9 @@ private Description checkForImmediateVariable( return NO_MATCH; } // TODO(ghm): Relax the requirement of this being an exact same symbol. Handle expressions? - if (!(state.getTypes().isSubtype(getType(instanceOfTree.getType()), getType(typeCast.getType())) + if (!(state + .getTypes() + .isSubtype(getType(instanceOfTree.getType()), getType(variableTree.getType())) && getSymbol(instanceOfTree.getExpression()) instanceof VarSymbol varSymbol && varSymbol.equals(getSymbol(typeCast.getExpression())))) { return NO_MATCH; diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java index 255528e55e6..422d7e30f34 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceofTest.java @@ -16,8 +16,6 @@ package com.google.errorprone.bugpatterns; -import static com.google.common.truth.TruthJUnit.assume; - import com.google.errorprone.BugCheckerRefactoringTestHelper; import org.junit.Test; import org.junit.runner.RunWith; @@ -216,7 +214,6 @@ void test(Object o) { @Test public void genericWithUpperBoundedWildcard() { - assume().that(Runtime.version().feature()).isAtLeast(21); helper .addInputLines( "Test.java", @@ -227,13 +224,11 @@ class Test { void test(Object object) { if (object instanceof List) { @SuppressWarnings("unchecked") - List list = (List) object; - System.err.println(list.get(0)); + List xs = (List) object; } } } """) - // TODO: b/380054832 - this shouldn't get re-written .addOutputLines( "Test.java", """ @@ -242,7 +237,8 @@ void test(Object object) { class Test { void test(Object object) { if (object instanceof List list) { - System.err.println(list.get(0)); + @SuppressWarnings("unchecked") + List xs = list; } } }