StreamToLoopInspection: reuse existing variable if we should reassign it in loop, but it's possible to use it as non-final

This commit is contained in:
Tagir Valeev
2016-12-23 13:30:08 +07:00
parent 4a2a11c85f
commit 1e33755e09
12 changed files with 147 additions and 27 deletions
@@ -0,0 +1,18 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
public class Main {
public void test(List<Integer> list) {
boolean x = false;
for (Integer i : list) {
if (i <= 2) {
x = true;
break;
}
}
if(x) {
System.out.println("found");
}
}
}
@@ -6,11 +6,10 @@ import java.util.stream.Stream;
public class Main<T> {
void test() {
Integer acc = 0;
Integer totalLength = 0;
for (String s : Arrays.asList("a", "bb", "ccc")) {
Integer length = s.length();
acc = acc + length;
totalLength = totalLength + length;
}
Integer totalLength = acc;
}
}
@@ -4,11 +4,10 @@ import java.util.List;
public class Main {
public void test(List<String> list) {
long count = 0;
long x = 0;
for (String s : list) {
count++;
x++;
}
long x = count;
System.out.println(x);
System.out.println(x);
}
}
@@ -0,0 +1,17 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
public class Main {
public void test(List<String> list) {
String res = "";
for (String s : list) {
String trim = s.trim();
if (!trim.isEmpty()) {
res = trim;
break;
}
}
System.out.println(res);
}
}
@@ -0,0 +1,19 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
public class Main {
public void test(List<String> list) {
// Cannot reuse the variable as user explicitly marked it as final
String found = "";
for (String s : list) {
String trim = s.trim();
if (!trim.isEmpty()) {
found = trim;
break;
}
}
final String res = found;
System.out.println(res);
}
}
@@ -1,6 +1,7 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.function.LongSupplier;
public class Main {
private static void test(List<String> list) {
@@ -12,7 +13,8 @@ public class Main {
}
long count = count1;
if(count > 10) {
long result = count*2;
LongSupplier sup = () -> count*2;
long result = sup.get();
System.out.println(result);
}
}
@@ -0,0 +1,12 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
public class Main {
public void test(List<Integer> list) {
boolean x = !list.stream().all<caret>Match(i -> i > 2);
if(x) {
System.out.println("found");
}
}
}
@@ -0,0 +1,10 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
public class Main {
public void test(List<String> list) {
String res = list.stream().map(String::trim).filter(trim -> !trim.isEmpty()).fi<caret>ndFirst().orElse("");
System.out.println(res);
}
}
@@ -0,0 +1,11 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
public class Main {
public void test(List<String> list) {
// Cannot reuse the variable as user explicitly marked it as final
final String res = list.stream().map(String::trim).filter(trim -> !trim.isEmpty()).fi<caret>ndFirst().orElse("");
System.out.println(res);
}
}
@@ -1,12 +1,14 @@
// "Replace Stream API chain with loop" "true"
import java.util.List;
import java.util.function.LongSupplier;
public class Main {
private static void test(List<String> list) {
long count = list.stream().filter(s -> !s.isEmpty()).co<caret>unt();
if(count > 10) {
long result = count*2;
LongSupplier sup = () -> count*2;
long result = sup.get();
System.out.println(result);
}
}