Correct handling of explicit variable type

This commit is contained in:
Valentin Kipyatkov
2016-10-15 22:57:56 +03:00
parent 9326dfa048
commit 5ca7688b7c
5 changed files with 35 additions and 0 deletions
@@ -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)
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+='"
// IS_APPLICABLE_2: false
import java.util.ArrayList
fun foo(map: Map<Int, String>) {
val result: ArrayList<String> = ArrayList()
<caret>for (s in map.values) {
result.add(s)
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+='"
// IS_APPLICABLE_2: false
import java.util.ArrayList
fun foo(map: Map<Int, String>) {
val result: ArrayList<String> = ArrayList()
result += map.values
}
@@ -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");
@@ -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");