IDEA-204917 "Loop can be collapsed with Stream API" creates uncompilable results in some cases

1. Look for references inside IntStream.iterate condition (Java9+)
2. When simplifying IntStream.range(...).map(i -> arr[i]) check if arr expression don't refer to i
This commit is contained in:
Tagir Valeev
2019-01-06 10:23:46 +07:00
parent 677af06353
commit 725cf20422
5 changed files with 42 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
// "Replace with reduce()" "false"
public class Foo {
boolean isAnyFalse(boolean[] array) {
boolean status = true;
f<caret>or (int i = 0; i < array.length & status; i++) {
status &= array[i];
}
return status;
}
}

View File

@@ -0,0 +1,12 @@
// "Replace with sum()" "true"
import java.util.stream.IntStream;
public class Matrix {
public double trace(final double[][] a) {
double sum = IntStream.range(0, a.length).mapToDouble(i -> a[i][i]).sum();
return sum;
}
}

View File

@@ -0,0 +1,14 @@
// "Replace with sum()" "true"
public class Matrix {
public double trace(final double[][] a) {
double sum = 0;
f<caret>or (int i = 0; i < a.length; i++) {
sum += a[i][i];
}
return sum;
}
}