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 cb3711df788..069e99d70d9 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 @@ -185,6 +185,9 @@ class AddToCollectionTransformation( val currentType = (initialization.variable.resolveToDescriptor() as VariableDescriptor).type if ((currentType.constructor.declarationDescriptor as? ClassDescriptor)?.importableFqName == newTypeFqName) return true // already of the required type + // we do not change explicit type + if (initialization.variable.typeReference != null) return false + if (initialization.initializationStatement != initialization.variable) return false val newTypeText = newTypeFqName.render() + IdeDescriptorRenderers.SOURCE_CODE.renderTypeArguments(currentType.arguments) diff --git a/idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt b/idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt new file mode 100644 index 00000000000..1814ed4a00d --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt @@ -0,0 +1,11 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with '+='" +// IS_APPLICABLE_2: false +import java.util.ArrayList + +fun foo(map: Map) { + val result: ArrayList = ArrayList() + for (s in map.values) { + result.add(s) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt.after b/idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt.after new file mode 100644 index 00000000000..5ed2bd09c75 --- /dev/null +++ b/idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt.after @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// INTENTION_TEXT: "Replace with '+='" +// IS_APPLICABLE_2: false +import java.util.ArrayList + +fun foo(map: Map) { + val result: ArrayList = ArrayList() + result += map.values +} \ 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 efe20633d01..312fda26a2a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTest2Generated.java @@ -1429,6 +1429,12 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 { doTest(fileName); } + @TestMetadata("explicitValType.kt") + public void testExplicitValType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt"); + doTest(fileName); + } + @TestMetadata("goodReceiver.kt") public void testGoodReceiver() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toCollection/goodReceiver.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index c13447c66cb..bd396b60cf8 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -9337,6 +9337,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("explicitValType.kt") + public void testExplicitValType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toCollection/explicitValType.kt"); + doTest(fileName); + } + @TestMetadata("goodReceiver.kt") public void testGoodReceiver() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/toCollection/goodReceiver.kt");