Do not convert anonymous object to lambda if its function is recursive
#KT-10348 Fixed
This commit is contained in:
@@ -60,6 +60,11 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
|
|||||||
if (!singleFunction.hasBody()) return null
|
if (!singleFunction.hasBody()) return null
|
||||||
if (singleFunction.valueParameters.any { it.name == null }) return null
|
if (singleFunction.valueParameters.any { it.name == null }) return null
|
||||||
|
|
||||||
|
// Recursive call, skip labels
|
||||||
|
if (ReferencesSearch.search(singleFunction, LocalSearchScope(singleFunction.bodyExpression!!)).any { it.element !is KtLabelReferenceExpression }) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
return TextRange(element.objectDeclaration.getObjectKeyword().startOffset, baseTypeRef.endOffset)
|
return TextRange(element.objectDeclaration.getObjectKeyword().startOffset, baseTypeRef.endOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(runnable: Runnable) {}
|
||||||
|
|
||||||
|
fun bar() {
|
||||||
|
foo(<caret>object : Runnable {
|
||||||
|
override fun run() {
|
||||||
|
run()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -6189,6 +6189,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("Recursive.kt")
|
||||||
|
public void testRecursive() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/objectLiteralToLambda/Recursive.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("ReturnNoValue.kt")
|
@TestMetadata("ReturnNoValue.kt")
|
||||||
public void testReturnNoValue() throws Exception {
|
public void testReturnNoValue() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/objectLiteralToLambda/ReturnNoValue.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/objectLiteralToLambda/ReturnNoValue.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user