KT-15789 Kotlin plugin incorrectly converts for-loops from Java to Kotlin

#KT-15789 fixed
This commit is contained in:
Simon Ogorodnik
2017-01-19 17:45:08 +03:00
committed by Simon Ogorodnik
parent bf3d4471cd
commit 82a70283b5
6 changed files with 61 additions and 8 deletions
@@ -86,12 +86,11 @@ object J2KPostProcessingRegistrar {
val variable = expression.parent as? KtProperty
if (variable != null && expression == variable.initializer && variable.isLocal) {
val refs = ReferencesSearch.search(variable, LocalSearchScope(variable.containingFile)).findAll()
for (ref in refs) {
val usage = ref.element as? KtSimpleNameExpression ?: continue
usage.replace(expression)
val ref = ReferencesSearch.search(variable, LocalSearchScope(variable.containingFile)).findAll().singleOrNull()
if (ref != null && ref.element is KtSimpleNameExpression) {
ref.element.replace(expression)
variable.delete()
}
variable.delete()
}
}
@@ -1,8 +1,9 @@
internal class C {
fun foo(o: Any) {
if (o is String) {
val l = o.length
val substring = o.substring(l - 2)
val s = o
val l = s.length
val substring = s.substring(l - 2)
}
}
}
}
@@ -0,0 +1,21 @@
import java.util.Iterator;
import java.util.List;
public class C {
public static void consume1(C c) {
}
public static void consume2(C c) {
}
public static void foo(List<C> cList) {
for (Iterator iter = cList.iterator(); iter.hasNext();) {
C c = (C) iter.next();
consume1(c);
consume2(c);
}
}
}
@@ -0,0 +1,19 @@
object C {
fun consume1(c: C) {
}
fun consume2(c: C) {
}
fun foo(cList: List<C>) {
val iter = cList.iterator()
while (iter.hasNext()) {
val c = iter.next()
consume1(c)
consume2(c)
}
}
}
@@ -4115,6 +4115,13 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("RedunduntTypeCastAndProhibitedInline.java")
public void testRedunduntTypeCastAndProhibitedInline() throws Exception {
String fileName = KotlinTestUtils
.navigationMetadata("j2k/testData/fileOrElement/postProcessing/RedunduntTypeCastAndProhibitedInline.java");
doTest(fileName);
}
@TestMetadata("SyntheticExtensionPropertyAccess.java")
public void testSyntheticExtensionPropertyAccess() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/SyntheticExtensionPropertyAccess.java");
@@ -4115,6 +4115,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
doTest(fileName);
}
@TestMetadata("RedunduntTypeCastAndProhibitedInline.java")
public void testRedunduntTypeCastAndProhibitedInline() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/RedunduntTypeCastAndProhibitedInline.java");
doTest(fileName);
}
@TestMetadata("SyntheticExtensionPropertyAccess.java")
public void testSyntheticExtensionPropertyAccess() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/SyntheticExtensionPropertyAccess.java");