IG: don't expect an array of generic type in "Suspicious 'Collection.toArray()' call" inspection

This commit is contained in:
Bas Leijdekkers
2015-12-09 18:05:26 +01:00
parent 37f833cad6
commit 7d7c49c5a6
2 changed files with 30 additions and 2 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2005-2013 Bas Leijdekkers
* Copyright 2005-2015 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import com.intellij.psi.util.InheritanceUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -124,6 +125,22 @@ public class SuspiciousToArrayCallInspection extends BaseInspection {
if (componentType.isAssignableFrom(parameter)) {
return;
}
if (parameter instanceof PsiClassType) {
final PsiClassType classType = (PsiClassType)parameter;
final PsiClass aClass = classType.resolve();
if (aClass instanceof PsiTypeParameter) {
final PsiTypeParameter typeParameter = (PsiTypeParameter)aClass;
final PsiReferenceList extendsList = typeParameter.getExtendsList();
final PsiClassType[] types = extendsList.getReferencedTypes();
if (types.length == 0) {
registerError(argument, TypeUtils.getObjectType(argument));
}
else if (types.length == 1) {
registerError(argument, types[0]);
}
return;
}
}
registerError(argument, parameter);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,17 @@ public class SuspiciousToArrayCallInspectionTest extends LightInspectionTestCase
"}");
}
public void testGenerics() {
doTest("import java.util.*;" +
"class K<T extends Integer> {\n" +
" List<T> list = new ArrayList<>();\n" +
"\n" +
" String[] m() {\n" +
" return list.toArray(/*Array of type 'java.lang.Integer[]' expected*/new String[list.size()]/**/);\n" +
" }\n" +
"}");
}
@Override
protected InspectionProfileEntry getInspection() {
return new SuspiciousToArrayCallInspection();