Skip to content

Commit

Permalink
Use inferred method argument types in targetType
Browse files Browse the repository at this point in the history
When computing the target type of an actual parameter, use the inferred parameter
type of the method invocation, and not the formal parameter type.

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201771839
  • Loading branch information
cushon committed Jun 29, 2018
1 parent 90d4d9e commit 1270bda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
20 changes: 11 additions & 9 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Expand Up @@ -1337,29 +1337,31 @@ public Type visitConditionalExpression(ConditionalExpressionTree tree, Void unus

@Override
public Type visitNewClass(NewClassTree tree, Void unused) {
return visitMethodInvocationOrNewClass(tree.getArguments(), ASTHelpers.getSymbol(tree));
return visitMethodInvocationOrNewClass(
tree.getArguments(), ASTHelpers.getSymbol(tree), ((JCNewClass) tree).constructorType);
}

@Override
public Type visitMethodInvocation(MethodInvocationTree tree, Void unused) {
return visitMethodInvocationOrNewClass(tree.getArguments(), ASTHelpers.getSymbol(tree));
return visitMethodInvocationOrNewClass(
tree.getArguments(), ASTHelpers.getSymbol(tree), ((JCMethodInvocation) tree).meth.type);
}

private Type visitMethodInvocationOrNewClass(
List<? extends ExpressionTree> arguments, MethodSymbol sym) {
List<? extends ExpressionTree> arguments, MethodSymbol sym, Type type) {
int idx = arguments.indexOf(current);
if (idx == -1) {
return null;
}
if (sym.getParameters().size() <= idx) {
if (type.getParameterTypes().size() <= idx) {
checkState(sym.isVarArgs());
idx = sym.getParameters().size() - 1;
idx = type.getParameterTypes().size() - 1;
}
Type type = sym.getParameters().get(idx).asType();
if (sym.isVarArgs() && idx == sym.getParameters().size() - 1) {
type = state.getTypes().elemtype(type);
Type argType = type.getParameterTypes().get(idx);
if (sym.isVarArgs() && idx == type.getParameterTypes().size() - 1) {
argType = state.getTypes().elemtype(argType);
}
return type;
return argType;
}

@Override
Expand Down
Expand Up @@ -17,8 +17,9 @@
package com.google.errorprone.util.testdata;

import java.io.Serializable;
import java.util.List;

class TargetTypeTest {
abstract class TargetTypeTest {
void unary() {
System.out.println(
// BUG: Diagnostic contains: boolean
Expand Down Expand Up @@ -361,6 +362,21 @@ void concatenation(String s, Object a) {
a = s + detectStringArray();
}

abstract <T> T id(T t);

abstract <T> List<T> list(List<T> t);

void generic() {
// BUG: Diagnostic contains: java.lang.String
String s = id(detectString());
// BUG: Diagnostic contains: java.lang.Integer
int i = id(detectPrimitiveInt());
// BUG: Diagnostic contains: java.util.List<java.lang.String>
List<String> y = id(detectStringList());
// BUG: Diagnostic contains: java.lang.Integer
Integer z = id(detectPrimitiveInt());
}

// Helper methods that we can search for.
static byte detectPrimitiveByte() {
return 0;
Expand Down Expand Up @@ -398,5 +414,9 @@ static Void detectVoid() {
return null;
}

static List<String> detectStringList() {
return null;
}

enum ThisEnum {}
}

0 comments on commit 1270bda

Please sign in to comment.