[kotlin] inline: ensure to put run's lambda inside parenthesis when it's located in super's delegate clause

^KTIJ-29163 fixed

GitOrigin-RevId: 7e07e921a67e8a6a2f93285028bcbaa38f6a1d5f
This commit is contained in:
Anna Kozlova
2024-03-22 19:11:12 +00:00
committed by intellij-monorepo-bot
parent d8c7446aa5
commit 74b00e0243
2 changed files with 7 additions and 5 deletions
@@ -1,5 +1,5 @@
interface DelegateFace
class DelegatingB : DelegateFace by run {
class DelegatingB : DelegateFace by run({
val result = object : DelegateFace {}
result
}
})
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
@@ -285,10 +286,11 @@ class ExpressionReplacementPerformer(
}
private fun KtElement.replaceWithRunBlockAndGetExpression(): KtExpression {
val runExpression = psiFactory.createExpressionByPattern("run { $0 }", this) as KtCallExpression
val runExpressionText = if (getStrictParentOfType<KtDelegatedSuperTypeEntry>() != null) "run({ $0 })" else "run { $0 }"
val runExpression = psiFactory.createExpressionByPattern(runExpressionText, this) as KtCallExpression
val runAfterReplacement = this.replaced(runExpression)
val ktLambdaArgument = runAfterReplacement.lambdaArguments[0]
return ktLambdaArgument.getLambdaExpression()?.bodyExpression?.statements?.singleOrNull()
val ktLambdaArgument = runAfterReplacement.valueArguments[0]
return (ktLambdaArgument.getArgumentExpression() as? KtLambdaExpression)?.bodyExpression?.statements?.singleOrNull()
?: throw KotlinExceptionWithAttachments("cant get body expression for $ktLambdaArgument")
.withPsiAttachment("ktLambdaArgument", ktLambdaArgument)
}