warn about raw arrays passed to varargs method (IDEA-16977)

This commit is contained in:
Anna Kozlova
2014-05-13 18:04:57 +04:00
parent b32b3332c4
commit 0feba8319e
4 changed files with 27 additions and 2 deletions
@@ -23,6 +23,7 @@ import com.intellij.codeInspection.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.util.InlineUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
@@ -108,8 +109,13 @@ public class RedundantArrayForVarargsCallInspection extends GenericsInspectionTo
PsiParameter lastParameter = parameters[args.length - 1];
PsiType lastParamType = lastParameter.getType();
LOG.assertTrue(lastParamType instanceof PsiEllipsisType);
if (!(lastArg instanceof PsiNewExpression) ||
!substitutor.substitute(((PsiEllipsisType)lastParamType).toArrayType()).equals(lastArg.getType())) {
if (!(lastArg instanceof PsiNewExpression)) {
return;
}
final PsiType substitutedLastParamType = substitutor.substitute(((PsiEllipsisType)lastParamType).toArrayType());
final PsiType lastArgType = lastArg.getType();
if (lastArgType == null || !lastArgType.equals(substitutedLastParamType) &&
!lastArgType.equals(TypeConversionUtil.erasure(substitutedLastParamType))) {
return;
}
PsiExpression[] initializers = getInitializers((PsiNewExpression)lastArg);
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>4</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,9 @@
public class Test {
{
try {
String.class.getConstructor(new Class[]{String.class});
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
@@ -23,4 +23,5 @@ public class RedundantArrayForVarargsCallInspectionTest extends InspectionTestCa
public void testNestedArray() throws Exception { doTest(); }
public void testCheckEnumConstant() throws Exception { doTest(); }
public void testGeneric() throws Exception { doTest(); }
public void testRawArray() throws Exception { doTest(); }
}