mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
BaseStreamApiMigration: when deleting the initializer replace type with explicit if possible
Fixes IDEA-193618 Invalid code generated after "Can be replaced with 'collect' call"
This commit is contained in:
+10
-1
@@ -17,6 +17,7 @@ package com.intellij.codeInspection.streamMigration;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ControlFlowUtils;
|
||||
import com.siyeh.ig.psiutils.ControlFlowUtils.InitializerUsageStatus;
|
||||
@@ -77,7 +78,15 @@ abstract class BaseStreamApiMigration {
|
||||
}
|
||||
else {
|
||||
if (status == ControlFlowUtils.InitializerUsageStatus.AT_WANTED_PLACE_ONLY) {
|
||||
ct.delete(initializer);
|
||||
PsiTypeElement typeElement = var.getTypeElement();
|
||||
if (typeElement != null && typeElement.isInferredType()) {
|
||||
if (PsiTypesUtil.isDenotableType(typeElement.getType(), var)) {
|
||||
PsiTypesUtil.replaceWithExplicitType(typeElement);
|
||||
ct.delete(initializer);
|
||||
}
|
||||
} else {
|
||||
ct.delete(initializer);
|
||||
}
|
||||
}
|
||||
return ct.replaceAndRestoreComments(loopStatement, var.getName() + " = " + replacement + ";");
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class Test {
|
||||
public List<Integer> someMethod() {
|
||||
ArrayList<Integer> listOfInteger;
|
||||
Integer[] arrayOfInteger = {2, 4, 8};
|
||||
listOfInteger = Arrays.stream(arrayOfInteger).filter(number -> number >= 4).collect(Collectors.toCollection(ArrayList::new));
|
||||
return listOfInteger;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Test {
|
||||
public List<Integer> someMethod() {
|
||||
var listOfInteger = new ArrayList<Integer>();
|
||||
Integer[] arrayOfInteger = {2, 4, 8};
|
||||
f<caret>or (var number: arrayOfInteger) {
|
||||
if (number >= 4) {
|
||||
listOfInteger.add(number);
|
||||
}
|
||||
}
|
||||
return listOfInteger;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user