KT-13482 "Loop can be replaced with stdlib operations" changes semantics

#KT-13482 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-08-17 18:06:34 +03:00
parent 38edb58e60
commit 26cdd0d5d5
5 changed files with 47 additions and 3 deletions
@@ -17,9 +17,9 @@
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtPsiUtil.isAssignment
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
class ForEachTransformation(
loop: KtForExpression,
@@ -53,6 +53,11 @@ class ForEachTransformation(
if (state.previousTransformations.isEmpty()) return null // do not suggest conversion to just ".forEach{}" or ".forEachIndexed{}"
val statement = state.statements.singleOrNull() ?: return null
// check if contains assignment to non-qualified variable - in this case only use of lazy sequence is correct
if (!state.lazySequence
&& statement.anyDescendantOfType<KtBinaryExpression> { isAssignment(it) && it.left is KtNameReferenceExpression }) return null
//TODO: should we disallow it for complicated statements like loops, if, when?
val transformation = ForEachTransformation(state.outerLoop, state.inputVariable, state.indexVariable, statement)
return TransformationMatch.Result(transformation)