RedundantCollectionOperationInspection: fix redundant asList type

This commit is contained in:
Tagir Valeev
2018-02-07 11:10:20 +07:00
parent d88539af20
commit 7e915181dc
3 changed files with 43 additions and 6 deletions
@@ -0,0 +1,17 @@
// "Unwrap" "true"
import java.util.Arrays;
import java.util.List;
class Foo {
interface Parent1 {}
interface Parent2 {}
interface Child1 extends Parent1, Parent2 {}
interface Child2 extends Parent1, Parent2 {}
void bar(boolean flag, Child1[] arr1, Child2[] arr2) {
Parent2[] list = flag ? arr1 : arr2;
for (Parent2 parent2 : list) {
System.out.println(parent2);
}
}
}
@@ -0,0 +1,17 @@
// "Unwrap" "true"
import java.util.Arrays;
import java.util.List;
class Foo {
interface Parent1 {}
interface Parent2 {}
interface Child1 extends Parent1, Parent2 {}
interface Child2 extends Parent1, Parent2 {}
void bar(boolean flag, Child1[] arr1, Child2[] arr2) {
List<? extends Parent2> list = Arrays.a<caret>sList(flag ? arr1 : arr2);
for (Parent2 parent2 : list) {
System.out.println(parent2);
}
}
}
@@ -413,14 +413,17 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca
if (parent instanceof PsiLocalVariable) {
PsiTypeElement typeElement = ((PsiLocalVariable)parent).getTypeElement();
if (!typeElement.isInferredType()) {
PsiType type = args[0].getType();
if (type == null) return;
if(type instanceof PsiEllipsisType) {
type = ((PsiEllipsisType)type).toArrayType();
PsiType varType = ((PsiLocalVariable)parent).getType();
PsiType elementType = PsiUtil.substituteTypeParameter(varType, CommonClassNames.JAVA_LANG_ITERABLE, 0, false);
if (elementType == null) {
PsiType type = args[0].getType();
if (!(type instanceof PsiArrayType)) return;
elementType = ((PsiArrayType)type).getComponentType();
}
if (!typeElement.isInferredType()) {
typeElement.replace(JavaPsiFacade.getElementFactory(project).createTypeElement(type));
if (elementType instanceof PsiWildcardType) {
elementType = ((PsiWildcardType)elementType).getExtendsBound();
}
typeElement.replace(JavaPsiFacade.getElementFactory(project).createTypeElement(elementType.createArrayType()));
}
}
ct.replaceAndRestoreComments(call, ct.markUnchanged(args[0]));