Cannot transform to flatMap if old working variable used in the nested loop
This commit is contained in:
+4
-2
@@ -64,12 +64,14 @@ class FlatMapTransformation(
|
|||||||
val iterableType = FuzzyType(builtIns.iterableType, builtIns.iterable.declaredTypeParameters)
|
val iterableType = FuzzyType(builtIns.iterableType, builtIns.iterable.declaredTypeParameters)
|
||||||
if (iterableType.checkIsSuperTypeOf(nestedSequenceType) == null) return null
|
if (iterableType.checkIsSuperTypeOf(nestedSequenceType) == null) return null
|
||||||
|
|
||||||
|
val nestedLoopBody = nestedLoop.body ?: return null
|
||||||
|
if (state.workingVariable.hasUsages(listOf(nestedLoopBody))) return null // workingVariable is still needed - cannot transform
|
||||||
|
|
||||||
val newWorkingVariable = nestedLoop.loopParameter ?: return null
|
val newWorkingVariable = nestedLoop.loopParameter ?: return null
|
||||||
val loopBody = nestedLoop.body ?: return null
|
|
||||||
val transformation = FlatMapTransformation(state.workingVariable, transform)
|
val transformation = FlatMapTransformation(state.workingVariable, transform)
|
||||||
val newState = state.copy(
|
val newState = state.copy(
|
||||||
innerLoop = nestedLoop,
|
innerLoop = nestedLoop,
|
||||||
statements = listOf(loopBody),
|
statements = listOf(nestedLoopBody),
|
||||||
workingVariable = newWorkingVariable
|
workingVariable = newWorkingVariable
|
||||||
)
|
)
|
||||||
return SequenceTransformationMatch(transformation, newState)
|
return SequenceTransformationMatch(transformation, newState)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
fun foo(list: List<String>): String? {
|
||||||
|
<caret>for (s in list) {
|
||||||
|
for (line in s.lines()) {
|
||||||
|
if (line.isNotBlank() && line.length < s.length / 10) return line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user