mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: don't expect an array of generic type in "Suspicious 'Collection.toArray()' call" inspection
This commit is contained in:
+18
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+12
-1
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user