diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java index d82f7586307c..e44a8a613c8a 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java @@ -643,6 +643,10 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo boolean isWriteAllowed(PsiVariable variable, PsiExpression reference) { return false; } + + boolean canReassignVariable(PsiVariable variable) { + return true; + } } static class FilterOp extends Operation { @@ -773,6 +777,11 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo return mySource.isWriteAllowed(variable, reference); } + @Override + boolean canReassignVariable(PsiVariable variable) { + return mySource.canReassignVariable(variable); + } + boolean breaksMe(PsiBreakStatement statement) { return statement.findExitedStatement() == mySource.getLoop(); } @@ -1054,6 +1063,11 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo return false; } + @Override + boolean canReassignVariable(PsiVariable variable) { + return variable != myVariable; + } + @Nullable public static CountingLoopSource from(PsiForStatement forStatement) { CountingLoop loop = CountingLoop.from(forStatement); diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/TerminalBlock.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/TerminalBlock.java index 326102070a43..4deaada7d342 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/TerminalBlock.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/TerminalBlock.java @@ -216,7 +216,7 @@ class TerminalBlock { } } PsiExpression rValue = ExpressionUtils.getAssignmentTo(first, myVariable); - if(rValue != null) { + if(rValue != null && operations().allMatch(op -> op.canReassignVariable(myVariable))) { PsiStatement[] leftOver = Arrays.copyOfRange(myStatements, 1, myStatements.length); MapOp op = new MapOp(rValue, myVariable, myVariable.getType()); return new TerminalBlock(this, op, myVariable, leftOver); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeRangeReassigned.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeRangeReassigned.java new file mode 100644 index 000000000000..f1a211bbe74d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeRangeReassigned.java @@ -0,0 +1,14 @@ +// "Replace with collect" "false" + +public class Test { + public void test(int n) { + for(int i=0; ii=0; i