[java-inspection] Support multi-line single-return lambdas in StreamToLoopInspection

Part of IDEABKL-7718
Fixes IDEA-317735

GitOrigin-RevId: 120245c2b1f4abb464d52c43dd39078a83f4bbcd
This commit is contained in:
Tagir Valeev
2023-04-14 14:47:54 +02:00
committed by intellij-monorepo-bot
parent 6076d18d3f
commit 78c8e66901
10 changed files with 241 additions and 119 deletions

View File

@@ -37,16 +37,17 @@ public class Main {
}
void sample3(List<String> people) {
List<String> list2 = people.stream().collect( // comment
Collectors.collectingAndThen(Collectors.<String, List<String>>toCollection(LinkedList::new),
list -> {
List<String> result = new ArrayList<>();
for (Iterator<String> it = Stream.concat(list.stream(), list.stream()).iterator(); it.hasNext(); ) {
String s = it.next();
result.add(s);
}
return result;
}));
// comment
List<String> strings = new LinkedList<>();
for (String person : people) {
strings.add(person);
}
List<String> result = new ArrayList<>();
for (Iterator<String> it = Stream.concat(strings.stream(), strings.stream()).iterator(); it.hasNext(); ) {
String s = it.next();
result.add(s);
}
List<String> list2 = result;
}
void sample4(List<String> people) {

View File

@@ -77,7 +77,7 @@ public class Main {
static Integer testReducing3() {
Integer totalLength = 0;
for (String s : Arrays.asList("a", "bb", "ccc")) {
for (String s : asList("a", "bb", "ccc")) {
Integer length = s.length();
totalLength = totalLength + length;
}

View File

@@ -11,12 +11,12 @@ import static java.util.Arrays.asList;
public class Main {
private static long testChain(List<? extends String> list) {
long count = 0L;
for (Object o : Arrays.asList(0, null, "1", list)) {
for (Object object : Arrays.asList(o)) {
for (Object o1 : Arrays.asList(object)) {
for (Object object1 : Arrays.asList(o1)) {
for (Object o2 : Arrays.asList(object1)) {
for (Object object2 : Arrays.asList(o2)) {
for (Object o : asList(0, null, "1", list)) {
for (Object object : asList(o)) {
for (Object o1 : asList(object)) {
for (Object object1 : asList(o1)) {
for (Object o2 : asList(object1)) {
for (Object object2 : asList(o2)) {
count++;
}
}

View File

@@ -0,0 +1,41 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.stream.*;
class X {
record N(N parent, List<N> children, String whatever) {}
private static N reproducer(N parent, String frame) {
for (N child : parent.children) {
if (child.whatever.equals(frame)) {
return child;
}
}
N result = new N(parent, new ArrayList<>(), frame);
parent.children.add(result);
return result;
}
void testMap(List<String> list) {
List<List<String>> newList = new ArrayList<>();
for (String s : list) {
List<String> result = new ArrayList<>();
result.add(s);
result.add(s + s);
List<String> apply = result;
newList.add(apply);
}
}
void testFilter(List<String> list) {
List<String> newList = new ArrayList<>();
for (String string : list) {
boolean result = false;
if (string.isEmpty()) result = true;
if (result) {
newList.add(string);
}
}
}
}

View File

@@ -65,7 +65,7 @@ public class Main {
map.put(true, new HashMap<>());
for (String string : strings) {
String s = string/*trimming*/.trim();
if (map.get(s.length() /*too big!*/ > 2).put(((UnaryOperator<String>) /* cast is necessary here */ x -> x).apply(s), s.length()) != null) {
if (map.get(s.length() /*too big!*/ > 2).put(((UnaryOperator<String>) /* cast is necessary here */ x -> x = x).apply(s), s.length()) != null) {
throw new IllegalStateException("Duplicate key");
}
}

View File

@@ -0,0 +1,40 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.stream.*;
class X {
record N(N parent, List<N> children, String whatever) {}
private static N reproducer(N parent, String frame) {
return parent.children.<caret>stream()
.filter(child -> child.whatever.equals(frame))
.findAny()
.orElseGet(() -> {
N result = new N(parent, new ArrayList<>(), frame);
parent.children.add(result);
return result;
});
}
void testMap(List<String> list) {
List<List<String>> newList = list.stream()
.map(item -> {
List<String> result = new ArrayList<>();
result.add(item);
result.add(item + item);
return result;
})
.collect(Collectors.toList());
}
void testFilter(List<String> list) {
List<String> newList = list.stream()
.filter(s -> {
boolean result = false;
if (s.isEmpty()) result = true;
return result;
})
.collect(Collectors.toList());
}
}

View File

@@ -34,7 +34,7 @@ public class Main {
public static void testToMapNameConflict(List<String> strings) {
System.out.println(strings.stream().map(x -> x/*trimming*/.trim()) // and collect
.collect(Collectors.partitioningBy(s -> s.length() /*too big!*/ > 2,
Collectors.toMap(s -> ((UnaryOperator<String>) /* cast is necessary here */ x -> x).apply(s),
Collectors.toMap(s -> ((UnaryOperator<String>) /* cast is necessary here */ x -> x = x).apply(s),
String::length))));
}