highlighting for calls which arguments have unknown type IDEA-77566

This commit is contained in:
Maxim.Medvedev
2012-04-03 16:52:03 +04:00
parent 7f656d7c24
commit 4215333bd5
9 changed files with 49 additions and 50 deletions
@@ -264,7 +264,8 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection {
final PsiElement constructor = constructorResolveResult.getElement();
if (constructor != null) {
checkConstructorApplicability(constructorResolveResult, refElement);
if (!checkConstructorApplicability(constructorResolveResult, refElement)) return;
if (!checkUnknownParams(refElement)) return;
}
else {
final GroovyResolveResult[] results = constructorCall.multiResolveConstructor();
@@ -275,7 +276,7 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection {
if (!checkConstructorApplicability(result, refElement)) return;
}
}
if (!checkUnknownParams(refElement)) return;
registerError(getElementToHighlight(refElement, argList), GroovyBundle.message("constructor.call.is.ambiguous"));
}
else {
@@ -419,12 +420,12 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection {
final PsiType type = referenceExpression.getType();
if (resolved != null ) {
if (resolved instanceof PsiMethod && !resolveResult.isInvokedOnProperty()) {
checkMethodApplicability(resolveResult, referenceExpression);
if (!checkMethodApplicability(resolveResult, referenceExpression)) return;
}
else {
checkCallApplicability(type, referenceExpression);
if (!checkCallApplicability(type, referenceExpression)) return;
}
if (!checkUnknownParams(referenceExpression)) return;
}
else if (results.length > 0) {
for (GroovyResolveResult result : results) {
@@ -436,6 +437,7 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection {
if (!checkCallApplicability(type, referenceExpression)) return;
}
}
if (!checkUnknownParams(referenceExpression)) return;
registerError(getElementToHighlight(referenceExpression, PsiUtil.getArgumentsList(referenceExpression)),
GroovyBundle.message("method.call.is.ambiguous"));
@@ -443,6 +445,20 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection {
}
}
private boolean checkUnknownParams(PsiElement place) {
PsiType[] args = PsiUtil.getArgumentTypes(place, true);
if (args == null) return true;
for (PsiType arg : args) {
if (arg == null) {
registerError(getElementToHighlight(place, PsiUtil.getArgumentsList(place)), GroovyBundle.message("cannot.infer.argument.types"),
LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.WEAK_WARNING);
return false;
}
}
return true;
}
private boolean checkCannotInferArgumentTypes(PsiElement referenceExpression) {
if (PsiUtil.getArgumentTypes(referenceExpression, true) != null) return false;
@@ -553,10 +569,12 @@ public class GroovyAssignabilityCheckInspection extends BaseInspection {
PsiElement resolved = result.getElement();
if (resolved instanceof PsiMethod && !result.isInvokedOnProperty()) {
if (!checkMethodApplicability(result, invokedExpr)) return false;
if (!checkUnknownParams(invokedExpr)) return false;
}
else if (resolved instanceof PsiField) {
if (!checkCallApplicability(((PsiField)resolved).getType(), invokedExpr)) return false;
}
if (!checkUnknownParams(invokedExpr)) return false;
}
if (calls.length == 0 && !(invokedExpr instanceof GrString)) {
registerCannotApplyError(invokedExpr, argumentTypes, invokedExpr.getText());
@@ -299,7 +299,7 @@ public class TypesUtil {
}
}
if (isClassType(rType, GroovyCommonClassNames.GROOVY_LANG_GSTRING)) {
if (isClassType(rType, GROOVY_LANG_GSTRING)) {
if (isAssignable(lType, GroovyPsiManager.getInstance(manager.getProject()).createTypeByFQClassName(JAVA_LANG_STRING, scope), manager, scope)) {
return true;
}
@@ -584,9 +584,9 @@ public class TypesUtil {
public static PsiClassType createListType(@NotNull PsiClass elements) {
JavaPsiFacade facade = JavaPsiFacade.getInstance(elements.getProject());
GlobalSearchScope resolveScope = elements.getResolveScope();
PsiClass listClass = facade.findClass(CommonClassNames.JAVA_UTIL_LIST, resolveScope);
PsiClass listClass = facade.findClass(JAVA_UTIL_LIST, resolveScope);
if (listClass == null) {
return facade.getElementFactory().createTypeByFQClassName(CommonClassNames.JAVA_UTIL_LIST, resolveScope);
return facade.getElementFactory().createTypeByFQClassName(JAVA_UTIL_LIST, resolveScope);
}
return facade.getElementFactory().createType(listClass, facade.getElementFactory().createType(elements));
}
@@ -596,12 +596,12 @@ public class TypesUtil {
JavaPsiFacade facade = JavaPsiFacade.getInstance(context.getProject());
GlobalSearchScope resolveScope = context.getResolveScope();
PsiClass setClass = facade.findClass(CommonClassNames.JAVA_UTIL_SET, resolveScope);
PsiClass setClass = facade.findClass(JAVA_UTIL_SET, resolveScope);
if (setClass != null) {
return facade.getElementFactory().createType(setClass, type);
}
return facade.getElementFactory().createTypeByFQClassName(CommonClassNames.JAVA_UTIL_SET, resolveScope);
return facade.getElementFactory().createTypeByFQClassName(JAVA_UTIL_SET, resolveScope);
}
public static Map<String, PsiClass> getSuperClassesWithCache(@NotNull PsiClass aClass) {
@@ -52,6 +52,7 @@ import java.util.*;
/**
* @author Maxim.Medvedev
*/
@SuppressWarnings("unchecked")
public class GrClosureSignatureUtil {
private GrClosureSignatureUtil() {
}
@@ -549,7 +550,7 @@ public class GrClosureSignatureUtil {
for (GrExpression expression : expressionArgs) {
PsiType type = expression.getType();
if (expression instanceof GrNewExpression && com.intellij.psi.util.PsiUtil.resolveClassInType(type) == null) {
if (partial && expression instanceof GrNewExpression && com.intellij.psi.util.PsiUtil.resolveClassInType(type) == null) {
type = null;
}
if (eraseArgs) {
@@ -649,24 +650,6 @@ public class GrClosureSignatureUtil {
return generateAllMethodSignaturesByClosureSignature(name, signature, PsiTypeParameter.EMPTY_ARRAY, PsiSubstitutor.EMPTY);
}
@Nullable
public static PsiType getTypeByTypeArg(ArgInfo<PsiType> arg, PsiManager manager, GlobalSearchScope resolveScope) {
if (arg.isMultiArg) {
if (arg.args.size() == 0) return PsiType.getJavaLangObject(manager, resolveScope).createArrayType();
PsiType leastUpperBound = null;
for (PsiType type : arg.args) {
leastUpperBound = TypesUtil.getLeastUpperBoundNullable(leastUpperBound, type, manager);
}
if (leastUpperBound == null) return null;
return leastUpperBound.createArrayType();
}
else {
if (arg.args.size() > 0) return arg.args.get(0);
return null;
}
}
@Nullable
public static PsiType getTypeByArg(ArgInfo<PsiElement> arg, PsiManager manager, GlobalSearchScope resolveScope) {
if (arg.isMultiArg) {
@@ -251,7 +251,7 @@ public class PsiUtil {
}
else {
if (type == null) {
result.add(nullAsBottom ? PsiType.NULL : TypesUtil.getJavaLangObject(expression));
result.add(nullAsBottom ? null : TypesUtil.getJavaLangObject(expression));
}
else {
if (stopAt == expression) {
@@ -84,7 +84,7 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
private void doTest(InspectionProfileEntry... tools) {
myFixture.enableInspections(tools);
myFixture.testHighlighting(true, false, false, getTestName(false) + ".groovy");
myFixture.testHighlighting(true, false, true, getTestName(false) + ".groovy");
}
public void testCircularInheritance() throws Throwable {
@@ -477,11 +477,11 @@ class A {
doTest(new GroovyUnresolvedAccessInspection());
}
public void testUnknownVarInArgList() {
public void testImplicitEnumCoercion() {
doTest(new GroovyAssignabilityCheckInspection());
}
public void testImplicitEnumCoercion() {
public void testUnknownVarInArgList() {
doTest(new GroovyAssignabilityCheckInspection());
}
@@ -1,12 +1,11 @@
package org.jetbrains.plugins.groovy.spock
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.util.containers.CollectionFactory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiVariable
import com.intellij.psi.CommonClassNames
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiVariable
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.util.containers.CollectionFactory
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUnresolvedAccessInspection
@@ -203,13 +202,13 @@ class FooSpec extends spock.lang.Specification {
public void testVariable_resolved() {
myFixture.enableInspections(GroovyAssignabilityCheckInspection, GroovyUnresolvedAccessInspection)
myFixture.configureByText("FooSpec.groovy", """
myFixture.configureByText("FooSpec.groovy", """\
class FooSpec extends spock.lang.Specification {
def "foo test"() {
String subscriber = <warning>Mock</warning>()
then: (0.._) * subscriber.concat(_)
subscriber.concat(<warning>asdasdasd</warning>)
subscriber.concat<warning>(23)</warning>
String subscriber = <warning descr="Can not resolve symbol 'Mock'">Mock</warning>()
then: (0.._) * subscriber.concat<weak_warning descr="Cannot infer argument types">(_)</weak_warning>
subscriber.concat<weak_warning descr="Cannot infer argument types">(<warning descr="Can not resolve symbol 'asdasdasd'">asdasdasd</warning>)</weak_warning>
subscriber.concat<warning descr="'concat' in 'java.lang.String' cannot be applied to '(java.lang.Integer)'">(23)</warning>
}
}
""")
@@ -5,5 +5,5 @@ enum My {
My var = 'foo'
var = <warning descr="Cannot find enum constant 'fail' in enum 'My'">'fail'</warning>
var = "fo"+"o"
var="fo${'o'}"
var = <weak_warning descr="Cannot assign string to enum 'My'">"fo"+"o"</weak_warning>
var=<weak_warning descr="Cannot assign string to enum 'My'">"fo${'o'}"</weak_warning>
@@ -4,5 +4,4 @@ foo(1, 2, '3')
foo(1, *[2, '3'])
foo<warning descr="'foo' in 'SpreadArguments' cannot be applied to '(java.lang.Integer, java.lang.Integer, java.lang.Integer)'">(1, *[2, 3])</warning>
def list = new ArrayList()
foo(1, *list)
foo<weak_warning descr="Cannot infer argument types">(1, *list)</weak_warning>
@@ -1,7 +1,7 @@
def foo(Date d) {}
def a = unknown
foo(a)
foo<weak_warning descr="Cannot infer argument types">(a)</weak_warning>
foo<warning descr="'foo' in 'UnknownVarInArgList' cannot be applied to '(java.lang.Integer)'">(1)</warning>
@@ -10,4 +10,4 @@ def abc(Date d){}
def abc(int i) {}
def x = unknown2
abc<warning descr="'abc' in 'UnknownVarInArgList' cannot be applied to '(null)'">(x)</warning>
abc<weak_warning descr="Cannot infer argument types">(x)</weak_warning>