Skip to content

Commit

Permalink
Worry about generics in PatternMatchingInstanceof.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699195541
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 22, 2024
1 parent be5c983 commit a42c33e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -216,7 +214,6 @@ void test(Object o) {

@Test
public void genericWithUpperBoundedWildcard() {
assume().that(Runtime.version().feature()).isAtLeast(21);
helper
.addInputLines(
"Test.java",
Expand All @@ -227,13 +224,11 @@ class Test {
void test(Object object) {
if (object instanceof List) {
@SuppressWarnings("unchecked")
List<? extends CharSequence> list = (List) object;
System.err.println(list.get(0));
List<? extends CharSequence> xs = (List) object;
}
}
}
""")
// TODO: b/380054832 - this shouldn't get re-written
.addOutputLines(
"Test.java",
"""
Expand All @@ -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<? extends CharSequence> xs = list;
}
}
}
Expand Down

0 comments on commit a42c33e

Please sign in to comment.