From bd3d5a9dbb45131fff6b43e97975a6c1b1c0e581 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 18 Mar 2019 18:27:13 +0900 Subject: [PATCH] Use withIndex intention: fix false positive for destructuring declaration #KT-30341 Fixed --- .../idea/intentions/loopToCallChain/matchAndConvert.kt | 1 + .../intentions/useWithIndex/destructuringDeclaration.kt | 8 ++++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 5 +++++ 3 files changed, 14 insertions(+) create mode 100644 idea/testData/intentions/useWithIndex/destructuringDeclaration.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt index 87c878a0fca..82dc02c12d3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt @@ -379,6 +379,7 @@ data class IntroduceIndexData( ) fun matchIndexToIntroduce(loop: KtForExpression, reformat: Boolean): IntroduceIndexData? { + if (loop.destructuringDeclaration != null) return null val (inputVariable, indexVariable) = extractLoopData(loop) ?: return null if (indexVariable != null) return null // loop is already with "withIndex" diff --git a/idea/testData/intentions/useWithIndex/destructuringDeclaration.kt b/idea/testData/intentions/useWithIndex/destructuringDeclaration.kt new file mode 100644 index 00000000000..035d079c4f9 --- /dev/null +++ b/idea/testData/intentions/useWithIndex/destructuringDeclaration.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +// IS_APPLICABLE: false +fun foo(list: List>) { + var index = 0 + for ((s1, s2) in list) { + index++ + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 556602efee0..c29a9571af6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -17553,6 +17553,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/useWithIndex/customTypeWithIterator.kt"); } + @TestMetadata("destructuringDeclaration.kt") + public void testDestructuringDeclaration() throws Exception { + runTest("idea/testData/intentions/useWithIndex/destructuringDeclaration.kt"); + } + @TestMetadata("indexIncrementTwice.kt") public void testIndexIncrementTwice() throws Exception { runTest("idea/testData/intentions/useWithIndex/indexIncrementTwice.kt");