IDEA-69795 (Quickfix "Remove explicit array creation" breaks code)

This commit is contained in:
Bas Leijdekkers
2011-05-20 17:33:37 +02:00
parent e49630a434
commit 417fb950bb
4 changed files with 44 additions and 2 deletions
@@ -16,6 +16,8 @@
package com.intellij.codeInspection.miscGenerics;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.ExpectedTypeInfo;
import com.intellij.codeInsight.ExpectedTypesProvider;
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.*;
import com.intellij.openapi.diagnostic.Logger;
@@ -125,17 +127,29 @@ public class RedundantArrayForVarargsCallInspection extends GenericsInspectionTo
if (arrayElements.length > 0) {
copyArgumentList.addRange(arrayElements[0], arrayElements[arrayElements.length - 1]);
}
final Project project = callExpression.getProject();
final JavaResolveResult resolveResult;
if (callExpression instanceof PsiEnumConstant) {
final PsiEnumConstant enumConstant = (PsiEnumConstant)callExpression;
final PsiClass containingClass = enumConstant.getContainingClass();
final JavaPsiFacade facade = JavaPsiFacade.getInstance(enumConstant.getProject());
final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
final PsiClassType classType = facade.getElementFactory().createType(containingClass);
resolveResult = facade.getResolveHelper().resolveConstructor(classType, copyArgumentList, enumConstant);
return resolveResult.isValidResult() && resolveResult.getElement() == oldRefMethod;
} else {
resolveResult = copy.resolveMethodGenerics();
if (!resolveResult.isValidResult() || resolveResult.getElement() != oldRefMethod) {
return false;
}
final ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes((PsiCallExpression) callExpression, false);
final PsiType expressionType = ((PsiCallExpression)copy).getType();
for (ExpectedTypeInfo expectedType : expectedTypes) {
if (!expectedType.getType().isAssignableFrom(expressionType)) {
return false;
}
}
return true;
}
return resolveResult.isValidResult() && resolveResult.getElement() == oldRefMethod;
}
catch (IncorrectOperationException e) {
return false;
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>10</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant array creation</problem_class>
<description>Redundant array creation for calling varargs method</description>
</problem>
</problems>
@@ -0,0 +1,18 @@
class Test {
class A {}
class B extends A {}
void f() {
B b = new B();
C<A> l = asC(new A[]{b});
A a = new A();
C<A> m = asC(new A[]{a});
}
public static <T> C<T> asC(T... ts) {
return null;
}
class C<T> {}
}
@@ -22,4 +22,5 @@ public class RedundantArrayForVarargsCallInspectionTest extends InspectionTestCa
public void testIDEADEV25923() throws Exception { doTest(); }
public void testNestedArray() throws Exception { doTest(); }
public void testCheckEnumConstant() throws Exception { doTest(); }
public void testGeneric() throws Exception { doTest(); }
}