diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToLabeledReturnFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToLabeledReturnFix.kt index d5b25c3cbf0..93ca545289f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToLabeledReturnFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToLabeledReturnFix.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.idea.quickfix import com.intellij.codeInsight.intention.IntentionAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import com.intellij.psi.PsiElement import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.idea.KotlinBundle @@ -63,28 +62,23 @@ class ChangeToLabeledReturnFix( override fun doCreateActions(diagnostic: Diagnostic): List { val element = diagnostic.psiElement as? KtElement ?: return emptyList() val context by lazy { element.analyze() } - val returnExpression = when (diagnostic.factory) { - Errors.RETURN_NOT_ALLOWED -> + Errors.RETURN_NOT_ALLOWED -> { diagnostic.psiElement as? KtReturnExpression - Errors.TYPE_MISMATCH, - Errors.CONSTANT_EXPECTED_TYPE_MISMATCH, - Errors.NULL_FOR_NONNULL_TYPE -> - getLambdaReturnExpression(diagnostic.psiElement, context) + } + Errors.TYPE_MISMATCH, Errors.CONSTANT_EXPECTED_TYPE_MISMATCH, Errors.NULL_FOR_NONNULL_TYPE -> { + val returnExpression = diagnostic.psiElement.getStrictParentOfType() ?: return emptyList() + val lambda = returnExpression.getStrictParentOfType() ?: return emptyList() + val lambdaReturnType = context[BindingContext.FUNCTION, lambda.functionLiteral]?.returnType ?: return emptyList() + val returnType = returnExpression.returnedExpression?.getType(context) ?: return emptyList() + if (!returnType.isSubtypeOf(lambdaReturnType)) return emptyList() + returnExpression + } else -> null } ?: return emptyList() - - val candidates = findAccessibleLabels(context, returnExpression) - return candidates.map { ChangeToLabeledReturnFix(returnExpression, labeledReturn = "return@${it.render()}") } - } - - private fun getLambdaReturnExpression(element: PsiElement, bindingContext: BindingContext): KtReturnExpression? { - val returnExpression = element.getStrictParentOfType() ?: return null - val lambda = returnExpression.getStrictParentOfType() ?: return null - val lambdaReturnType = bindingContext[BindingContext.FUNCTION, lambda.functionLiteral]?.returnType ?: return null - val returnType = returnExpression.returnedExpression?.getType(bindingContext) ?: return null - if (!returnType.isSubtypeOf(lambdaReturnType)) return null - return returnExpression + return findAccessibleLabels(context, returnExpression).map { + ChangeToLabeledReturnFix(returnExpression, labeledReturn = "return@${it.render()}") + } } } } \ No newline at end of file