Renamed method
This commit is contained in:
+5
-5
@@ -122,7 +122,7 @@ class AddToCollectionTransformation(
|
||||
targetCollection: KtExpression,
|
||||
addOperationArgument: KtExpression
|
||||
): TransformationMatch.Result? {
|
||||
val collectionInitialization = targetCollection.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true) ?: return null
|
||||
val collectionInitialization = targetCollection.findVariableInitializationBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true) ?: return null
|
||||
val collectionKind = collectionInitialization.initializer.isSimpleCollectionInstantiation() ?: return null
|
||||
val argumentIsInputVariable = addOperationArgument.isVariableReference(state.inputVariable)
|
||||
when (collectionKind) {
|
||||
@@ -230,7 +230,7 @@ class FilterToTransformation private constructor(
|
||||
condition: KtExpression,
|
||||
isInverse: Boolean
|
||||
): ResultTransformation {
|
||||
val initialization = targetCollection.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
val initialization = targetCollection.findVariableInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
||||
val transformation = FilterToTransformation(loop, inputVariable, indexVariable, initialization.initializer, condition, isInverse)
|
||||
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||
@@ -259,7 +259,7 @@ class FilterNotNullToTransformation private constructor(
|
||||
loop: KtForExpression,
|
||||
targetCollection: KtExpression
|
||||
): ResultTransformation {
|
||||
val initialization = targetCollection.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
val initialization = targetCollection.findVariableInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
||||
val transformation = FilterNotNullToTransformation(loop, initialization.initializer)
|
||||
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||
@@ -302,7 +302,7 @@ class MapToTransformation private constructor(
|
||||
mapping: KtExpression,
|
||||
mapNotNull: Boolean
|
||||
): ResultTransformation {
|
||||
val initialization = targetCollection.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
val initialization = targetCollection.findVariableInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
||||
val transformation = MapToTransformation(loop, inputVariable, indexVariable, initialization.initializer, mapping, mapNotNull)
|
||||
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||
@@ -341,7 +341,7 @@ class FlatMapToTransformation private constructor(
|
||||
targetCollection: KtExpression,
|
||||
transform: KtExpression
|
||||
): ResultTransformation {
|
||||
val initialization = targetCollection.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
val initialization = targetCollection.findVariableInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
||||
val transformation = FlatMapToTransformation(loop, inputVariable, initialization.initializer, transform)
|
||||
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ class CountTransformation(
|
||||
|
||||
override fun match(state: MatchingState): TransformationMatch.Result? {
|
||||
val operand = state.statements.singleOrNull()?.isPlusPlusOf() ?: return null
|
||||
val initialization = operand.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true) ?: return null
|
||||
val initialization = operand.findVariableInitializationBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true) ?: return null
|
||||
|
||||
if (initialization.variable.countUsages(state.outerLoop) != 1) return null // this should be the only usage of this variable inside the loop
|
||||
|
||||
|
||||
+1
-1
@@ -81,7 +81,7 @@ object FindTransformationMatcher : TransformationMatcher {
|
||||
val left = binaryExpression.left ?: return null
|
||||
val right = binaryExpression.right ?: return null
|
||||
|
||||
val initialization = left.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true) ?: return null
|
||||
val initialization = left.findVariableInitializationBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true) ?: return null
|
||||
|
||||
if (initialization.variable.countUsages(state.outerLoop) != 1) return null // this should be the only usage of this variable inside the loop
|
||||
|
||||
|
||||
+2
-2
@@ -101,7 +101,7 @@ class MaxOrMinTransformation(
|
||||
val assignment = state.statements.singleOrNull() as? KtBinaryExpression ?: return null
|
||||
if (assignment.operationToken != KtTokens.EQ) return null
|
||||
|
||||
val variableInitialization = assignment.left.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
||||
val variableInitialization = assignment.left.findVariableInitializationBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
||||
?: return null
|
||||
|
||||
return matchMathMaxOrMin(variableInitialization, assignment, state, isMax = true)
|
||||
@@ -159,7 +159,7 @@ class MaxOrMinTransformation(
|
||||
return null
|
||||
}
|
||||
|
||||
val variableInitialization = otherHand.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = false)
|
||||
val variableInitialization = otherHand.findVariableInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = false)
|
||||
?: return null
|
||||
|
||||
if (!assignmentTarget.isVariableReference(variableInitialization.variable)) return null
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ abstract class SumTransformationBase(
|
||||
val statement = state.statements.singleOrNull() as? KtBinaryExpression ?: return null
|
||||
if (statement.operationToken != KtTokens.PLUSEQ) return null
|
||||
|
||||
val variableInitialization = statement.left.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true)
|
||||
val variableInitialization = statement.left.findVariableInitializationBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true)
|
||||
?: return null
|
||||
|
||||
val value = statement.right ?: return null
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ object IntroduceIndexMatcher : TransformationMatcher {
|
||||
private fun checkIndexCandidate(incrementExpression: KtUnaryExpression, state: MatchingState): TransformationMatch.Sequence? {
|
||||
val operand = incrementExpression.isPlusPlusOf() ?: return null
|
||||
|
||||
val variableInitialization = operand.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
||||
val variableInitialization = operand.findVariableInitializationBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
||||
?: return null
|
||||
if ((variableInitialization.initializer as? KtConstantExpression)?.text != "0") return null
|
||||
|
||||
|
||||
@@ -141,7 +141,7 @@ data class VariableInitialization(
|
||||
val initializer: KtExpression)
|
||||
|
||||
//TODO: we need more correctness checks (if variable is non-local or is local but can be changed by some local functions)
|
||||
fun KtExpression?.isVariableInitializedBeforeLoop(
|
||||
fun KtExpression?.findVariableInitializationBeforeLoop(
|
||||
loop: KtForExpression,
|
||||
checkNoOtherUsagesInLoop: Boolean
|
||||
): VariableInitialization? {
|
||||
|
||||
Reference in New Issue
Block a user