diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAsListForEachWrongType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAsListForEachWrongType.java new file mode 100644 index 000000000000..2d14848d7db0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAsListForEachWrongType.java @@ -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); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAsListForEachWrongType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAsListForEachWrongType.java new file mode 100644 index 000000000000..acf0159df2b7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAsListForEachWrongType.java @@ -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 list = Arrays.asList(flag ? arr1 : arr2); + for (Parent2 parent2 : list) { + System.out.println(parent2); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java index 4508f3925467..6aebf4b0be74 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java @@ -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]));