KT-15789 Kotlin plugin incorrectly converts for-loops from Java to Kotlin
#KT-15789 fixed
This commit is contained in:
committed by
Simon Ogorodnik
parent
bf3d4471cd
commit
82a70283b5
@@ -86,12 +86,11 @@ object J2KPostProcessingRegistrar {
|
|||||||
|
|
||||||
val variable = expression.parent as? KtProperty
|
val variable = expression.parent as? KtProperty
|
||||||
if (variable != null && expression == variable.initializer && variable.isLocal) {
|
if (variable != null && expression == variable.initializer && variable.isLocal) {
|
||||||
val refs = ReferencesSearch.search(variable, LocalSearchScope(variable.containingFile)).findAll()
|
val ref = ReferencesSearch.search(variable, LocalSearchScope(variable.containingFile)).findAll().singleOrNull()
|
||||||
for (ref in refs) {
|
if (ref != null && ref.element is KtSimpleNameExpression) {
|
||||||
val usage = ref.element as? KtSimpleNameExpression ?: continue
|
ref.element.replace(expression)
|
||||||
usage.replace(expression)
|
variable.delete()
|
||||||
}
|
}
|
||||||
variable.delete()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
internal class C {
|
internal class C {
|
||||||
fun foo(o: Any) {
|
fun foo(o: Any) {
|
||||||
if (o is String) {
|
if (o is String) {
|
||||||
val l = o.length
|
val s = o
|
||||||
val substring = o.substring(l - 2)
|
val l = s.length
|
||||||
|
val substring = s.substring(l - 2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+21
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
@@ -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);
|
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")
|
@TestMetadata("SyntheticExtensionPropertyAccess.java")
|
||||||
public void testSyntheticExtensionPropertyAccess() throws Exception {
|
public void testSyntheticExtensionPropertyAccess() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/SyntheticExtensionPropertyAccess.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/SyntheticExtensionPropertyAccess.java");
|
||||||
|
|||||||
@@ -4115,6 +4115,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("SyntheticExtensionPropertyAccess.java")
|
||||||
public void testSyntheticExtensionPropertyAccess() throws Exception {
|
public void testSyntheticExtensionPropertyAccess() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/SyntheticExtensionPropertyAccess.java");
|
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/postProcessing/SyntheticExtensionPropertyAccess.java");
|
||||||
|
|||||||
Reference in New Issue
Block a user