From 94b8614fb80689131a3e03791d6efdec404cf769 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 14 Dec 2017 17:59:22 +0100 Subject: [PATCH] Don't suggest `mapTo()` transformation over range literals #KT-18816 Fixed --- .../result/AddToCollectionTransformation.kt | 13 ++++++++++--- .../intentions/loopToCallChain/map/KT18816.kt | 10 ++++++++++ .../idea/intentions/IntentionTest2Generated.java | 6 ++++++ .../idea/intentions/IntentionTestGenerated.java | 6 ++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/loopToCallChain/map/KT18816.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt index d9860709e88..fdb48eacc44 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.imports.importableFqName import org.jetbrains.kotlin.idea.intentions.loopToCallChain.* import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.* import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression @@ -112,9 +113,13 @@ class AddToCollectionTransformation( TransformationMatch.Result(AddToCollectionTransformation(state.outerLoop, targetCollection)) } else { - //TODO: recognize "?: continue" in the argument - TransformationMatch.Result(MapToTransformation.create( - state.outerLoop, state.inputVariable, state.indexVariable, targetCollection, argumentValue, mapNotNull = false)) + if (state.outerLoop.loopRange.isRangeLiteral()) + null + else { + //TODO: recognize "?: continue" in the argument + TransformationMatch.Result(MapToTransformation.create( + state.outerLoop, state.inputVariable, state.indexVariable, targetCollection, argumentValue, mapNotNull = false)) + } } } @@ -197,6 +202,8 @@ class AddToCollectionTransformation( } } +private fun KtExpression?.isRangeLiteral() = this is KtBinaryExpression && operationToken == KtTokens.RANGE + class FilterToTransformation private constructor( loop: KtForExpression, private val inputVariable: KtCallableDeclaration, diff --git a/idea/testData/intentions/loopToCallChain/map/KT18816.kt b/idea/testData/intentions/loopToCallChain/map/KT18816.kt new file mode 100644 index 00000000000..4312e3143f8 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/map/KT18816.kt @@ -0,0 +1,10 @@ +// WITH_RUNTIME +// IS_APPLICABLE: false +// IS_APPLICABLE_2: false +fun test() { + val a = ArrayList() + for (i in 1..100) { + a.add(i + 1) + } + println(a) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java index fc5ef837eb3..81de09adea3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java @@ -1013,6 +1013,12 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 { doTest(fileName); } + @TestMetadata("KT18816.kt") + public void testKT18816() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/map/KT18816.kt"); + doTest(fileName); + } + @TestMetadata("map.kt") public void testMap() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/map/map.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 6fd624057c1..38f91c8891b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10924,6 +10924,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("KT18816.kt") + public void testKT18816() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/map/KT18816.kt"); + doTest(fileName); + } + @TestMetadata("map.kt") public void testMap() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/map/map.kt");