IDEA-231237 Loop-to-stream conversion produces incorrect code if pattern variable is introduced

Also: pattern variable can be deleted via simple .delete()

GitOrigin-RevId: d3523f64d86de28d90b82d7bd98c1fd70567652d
This commit is contained in:
Tagir Valeev
2020-01-26 10:14:30 +00:00
committed by intellij-monorepo-bot
parent ede907a364
commit 26221c0641
16 changed files with 170 additions and 37 deletions
@@ -0,0 +1,10 @@
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
class X {
void test(List<Object> list) {
List<String> result = list.stream().filter(o -> o instanceof String).map(o -> (String) o).collect(Collectors.toList());
}
}
@@ -0,0 +1,12 @@
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
class X {
void test(List<Object> list) {
List<Object> result = list.stream().filter(o -> getObject(o) instanceof String s && !s.isEmpty()).collect(Collectors.toList());
}
native Object getObject(Object obj);
}
@@ -0,0 +1,10 @@
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
class X {
void test(List<Object> list) {
List<String> result = list.stream().filter(o -> o instanceof String s && !s.isEmpty()).map(o -> (String) o).collect(Collectors.toList());
}
}
@@ -0,0 +1,14 @@
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
import java.util.List;
import java.util.ArrayList;
class X {
void test(List<Object> list) {
List<String> result = new ArrayList<>();
f<caret>or (Object o : list) {
if (o instanceof String s) {
result.add(s);
}
}
}
}
@@ -0,0 +1,16 @@
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
import java.util.List;
import java.util.ArrayList;
class X {
void test(List<Object> list) {
List<Object> result = new ArrayList<>();
f<caret>or (Object o : list) {
if (getObject(o) instanceof String s && !s.isEmpty()) {
result.add(o);
}
}
}
native Object getObject(Object obj);
}
@@ -0,0 +1,14 @@
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
import java.util.List;
import java.util.ArrayList;
class X {
void test(List<Object> list) {
List<String> result = new ArrayList<>();
f<caret>or (Object o : list) {
if (o instanceof String s && !s.isEmpty()) {
result.add(s);
}
}
}
}