mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-160778 Buggy replacement with collect when several variables are declared in the same statement
This commit is contained in:
+18
-12
@@ -495,18 +495,24 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
final PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression();
|
||||
if (qualifierExpression instanceof PsiReferenceExpression) {
|
||||
final PsiElement resolve = ((PsiReferenceExpression)qualifierExpression).resolve();
|
||||
if (resolve instanceof PsiVariable) {
|
||||
if (resolve instanceof PsiLocalVariable && foreachStatement.equals(PsiTreeUtil.skipSiblingsForward(resolve.getParent(), PsiWhiteSpace.class, PsiComment.class))) {
|
||||
final PsiExpression initializer = ((PsiVariable)resolve).getInitializer();
|
||||
if (initializer instanceof PsiNewExpression) {
|
||||
final PsiExpressionList argumentList = ((PsiNewExpression)initializer).getArgumentList();
|
||||
if (argumentList != null && argumentList.getExpressions().length == 0) {
|
||||
restoreComments(foreachStatement, body);
|
||||
final String callText = builder.toString() + createInitializerReplacementText(((PsiVariable)resolve).getType(), initializer) + ")";
|
||||
result = initializer.replace(elementFactory.createExpressionFromText(callText, null));
|
||||
simplifyRedundantCast(result);
|
||||
foreachStatement.delete();
|
||||
return;
|
||||
if (resolve instanceof PsiLocalVariable) {
|
||||
PsiLocalVariable var = (PsiLocalVariable)resolve;
|
||||
PsiElement declaration = var.getParent();
|
||||
if (declaration instanceof PsiDeclarationStatement) {
|
||||
PsiElement[] elements = ((PsiDeclarationStatement)declaration).getDeclaredElements();
|
||||
if (elements[elements.length - 1] == resolve &&
|
||||
foreachStatement.equals(PsiTreeUtil.skipSiblingsForward(declaration, PsiWhiteSpace.class, PsiComment.class))) {
|
||||
final PsiExpression initializer = var.getInitializer();
|
||||
if (initializer instanceof PsiNewExpression) {
|
||||
final PsiExpressionList argumentList = ((PsiNewExpression)initializer).getArgumentList();
|
||||
if (argumentList != null && argumentList.getExpressions().length == 0) {
|
||||
restoreComments(foreachStatement, body);
|
||||
final String callText = builder.toString() + createInitializerReplacementText(var.getType(), initializer) + ")";
|
||||
result = initializer.replace(elementFactory.createExpressionFromText(callText, null));
|
||||
simplifyRedundantCast(result);
|
||||
foreachStatement.delete();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Collect {
|
||||
class Person {
|
||||
String getName() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<String> names = new ArrayList<>(), otherNames = new ArrayList<>(names);
|
||||
names.addAll(persons.stream().map(Person::getName).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Collect {
|
||||
class Person {
|
||||
String getName() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<String> names = new ArrayList<>(), otherNames = new ArrayList<>(names);
|
||||
for (Person person : pers<caret>ons) {
|
||||
names.add(person.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user