mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+1
@@ -1481,6 +1481,7 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal
|
||||
PsiArrayType arrayType = tryCast(arrayExpr.getType(), PsiArrayType.class);
|
||||
if (arrayType == null) return null;
|
||||
if (!StreamApiUtil.isSupportedStreamElement(arrayType.getComponentType())) return null;
|
||||
if (VariableAccessUtils.variableIsUsed(parameters[0], arrayExpr)) return null;
|
||||
|
||||
PsiExpression leftBound = args[0];
|
||||
PsiExpression rightBound = args[1];
|
||||
|
||||
+2
-2
@@ -834,7 +834,7 @@ public class StreamApiMigrationInspection extends AbstractBaseJavaLocalInspectio
|
||||
}
|
||||
|
||||
boolean breaksMe(PsiBreakStatement statement) {
|
||||
return statement.findExitedStatement() == mySource.getMainStatement();
|
||||
return statement.findExitedElement() == mySource.getMainStatement();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1295,7 +1295,7 @@ public class StreamApiMigrationInspection extends AbstractBaseJavaLocalInspectio
|
||||
|
||||
@Override
|
||||
StreamEx<PsiExpression> expressions() {
|
||||
return StreamEx.of(myInitializer, myExpression);
|
||||
return StreamEx.of(myInitializer, myExpression, myCondition).nonNull();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+13
@@ -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;
|
||||
}
|
||||
}
|
||||
+12
@@ -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;
|
||||
}
|
||||
}
|
||||
+14
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user