[java-inspections] IDEA-373515 Fix 'Collapse loop with stream forEach()' breaking when static star import is used

Thanks for help, Bas!

Co-authored-by: Bas Leijdekkers <bas.leijdekkers@jetbrains.com>

GitOrigin-RevId: 1ab70d08fe4a1ea0c050d725bc0ec5149d019b7c
This commit is contained in:
Bartek Pacia
2025-05-27 18:07:39 +02:00
committed by intellij-monorepo-bot
parent 07634e6b2d
commit 79ad940ac3
7 changed files with 107 additions and 1 deletions

View File

@@ -457,7 +457,7 @@ final class TerminalBlock {
if (myStatements.length == 1) {
return myStatements[0];
}
PsiCodeBlock block = factory.createCodeBlock();
PsiCodeBlock block = factory.createCodeBlockFromText("{}", myVariable);
for (PsiStatement statement : myStatements) {
block.add(ct.markUnchanged(statement));
}

View File

@@ -0,0 +1,18 @@
// "Collapse loop with stream 'forEach()'" "true-preview"
import java.util.Objects;
import java.util.stream.IntStream;
public class Test {
void collectNames() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
deepEquals(arr1, arr2);
IntStream.range(0, arr1.length).forEach(i -> {
int a = arr1[i];
int b = arr2[i];
boolean result = Objects.deepEquals(a, b);
System.out.println("result is " + result);
});
}
}

View File

@@ -0,0 +1,18 @@
// "Collapse loop with stream 'forEach()'" "true-preview"
import java.util.Objects;
import java.util.stream.IntStream;
public class Test {
void collectNames() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
deepEquals(arr1, arr2);
IntStream.range(0, arr1.length).forEach(i -> {
int a = arr1[i];
int b = arr2[i];
boolean result = Objects.deepEquals(a, b);
System.out.println("result is " + result);
});
}
}

View File

@@ -0,0 +1,20 @@
// "Collapse loop with stream 'forEach()'" "true-preview"
import java.util.stream.IntStream;
import static java.util.Objects.*;
public class Test {
void collectNames() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
deepEquals(arr1, arr2);
IntStream.range(0, arr1.length).forEach(i -> {
int a = arr1[i];
int b = arr2[i];
boolean result = deepEquals(a, b);
System.out.println("result is " + result);
});
}
}

View File

@@ -0,0 +1,15 @@
// "Collapse loop with stream 'forEach()'" "true-preview"
public class Test {
void collectNames() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
deepEquals(arr1, arr2);
for <caret>(int i = 0; i < arr1.length; i++) {
int a = arr1[i];
int b = arr2[i];
boolean result = java.util.Objects.deepEquals(a, b);
System.out.println("result is " + result);
}
}
}

View File

@@ -0,0 +1,17 @@
// "Collapse loop with stream 'forEach()'" "true-preview"
import java.util.Objects;
public class Test {
void collectNames() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
deepEquals(arr1, arr2);
for <caret>(int i = 0; i < arr1.length; i++) {
int a = arr1[i];
int b = arr2[i];
boolean result = Objects.deepEquals(a, b);
System.out.println("result is " + result);
}
}
}

View File

@@ -0,0 +1,18 @@
// "Collapse loop with stream 'forEach()'" "true-preview"
import static java.util.Objects.*;
public class Test {
void collectNames() {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
deepEquals(arr1, arr2);
for <caret>(int i = 0; i < arr1.length; i++) {
int a = arr1[i];
int b = arr2[i];
boolean result = deepEquals(a, b);
System.out.println("result is " + result);
}
}
}