Skip to content

Commit

Permalink
Fix subtype test in UnnecessaryMethodReference
Browse files Browse the repository at this point in the history
`ASTHelpers.isSubtype` does erasure, which isn't what we want here.

Fixes #2117

PiperOrigin-RevId: 352601858
  • Loading branch information
cushon authored and Error Prone Team committed Jan 19, 2021
1 parent d847960 commit be94375
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.isSubtype;
import static com.google.errorprone.util.ASTHelpers.targetType;
import static javax.lang.model.element.Modifier.ABSTRACT;

Expand Down Expand Up @@ -74,7 +73,7 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s
&& ((IdentifierTree) receiver).getName().contentEquals("super")) {
return NO_MATCH;
}
if (!isSubtype(getType(receiver), targetType.type(), state)) {
if (!state.getTypes().isSubtype(getType(receiver), targetType.type())) {
return NO_MATCH;
}
MethodSymbol symbol = getSymbol(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,19 @@ public void ignoreSuper() {
"}")
.doTest();
}

@Test
public void subType() {
helper
.addSourceLines(
"T.java",
"import java.util.function.Consumer;",
"abstract class T {",
" void f(Consumer<String> c) {}",
" void g(Consumer<Object> c) {",
" f(c::accept);",
" }",
"}")
.doTest();
}
}

0 comments on commit be94375

Please sign in to comment.