Moved check about old working variable not needed into common place

This commit is contained in:
Valentin Kipyatkov
2016-04-06 16:41:17 +03:00
parent 22fb397662
commit df08493337
3 changed files with 5 additions and 3 deletions
@@ -82,8 +82,12 @@ fun match(loop: KtForExpression): ResultTransformationMatch? {
for (matcher in MatcherRegistrar.sequenceMatchers) {
val match = matcher.match(state)
if (match != null) {
val newState = match.newState
// check that old working variable is not needed anymore
if (state.workingVariable != newState.workingVariable && state.workingVariable.hasUsages(newState.statements)) return null
sequenceTransformations.addAll(match.transformations)
state = match.newState
state = newState
continue@MatchLoop
}
}
@@ -61,7 +61,6 @@ class FlatMapTransformation(
if (iterableType.checkIsSuperTypeOf(nestedSequenceType) == null) return null
val nestedLoopBody = nestedLoop.body ?: return null
if (state.workingVariable.hasUsages(nestedLoopBody)) return null // workingVariable is still needed - cannot transform
val newWorkingVariable = nestedLoop.loopParameter ?: return null
val transformation = FlatMapTransformation(state.workingVariable, transform)
@@ -49,7 +49,6 @@ class MapTransformation(
val initializer = declaration.initializer ?: return null
if (declaration.hasWriteUsages()) return null
val restStatements = state.statements.drop(1)
if (state.workingVariable.hasUsages(restStatements)) return null // workingVariable is still needed - cannot transform to map()
val transformation = MapTransformation(state.workingVariable, initializer)
val newState = state.copy(statements = restStatements, workingVariable = declaration)