diff --git a/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/RedundantReturnLabelInspection.kt b/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/RedundantReturnLabelInspection.kt index 1e16ecf45af2..21be9ec4f350 100644 --- a/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/RedundantReturnLabelInspection.kt +++ b/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/RedundantReturnLabelInspection.kt @@ -4,6 +4,7 @@ package org.jetbrains.kotlin.idea.codeInsight.inspections.shared import com.intellij.codeInspection.IntentionWrapper import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.PsiElementVisitor +import org.jetbrains.kotlin.analysis.api.analyze import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection import org.jetbrains.kotlin.idea.codeinsights.impl.base.quickFix.RemoveReturnLabelFix @@ -15,7 +16,11 @@ class RedundantReturnLabelInspection : AbstractKotlinInspection() { fun(returnExpression) { val label = returnExpression.getTargetLabel() ?: return val function = returnExpression.getParentOfType(true, KtLambdaExpression::class.java) ?: return - if (function.name == null) return + + if (function.name == null && + analyze(returnExpression) { returnExpression.getReturnTargetSymbol() != function.getSymbol() } + ) return + val labelName = label.getReferencedName() holder.registerProblem( label, diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java index c65f0ba5322f..bf80b6eb8995 100644 --- a/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java @@ -256,6 +256,11 @@ public abstract class SharedK1LocalInspectionTestGenerated extends AbstractShare runTest("../testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt"); } + @TestMetadata("inAnonymousFunction2.kt") + public void testInAnonymousFunction2() throws Exception { + runTest("../testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction2.kt"); + } + @TestMetadata("inFunction.kt") public void testInFunction() throws Exception { runTest("../testData/inspectionsLocal/redundantReturnLabel/inFunction.kt"); diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java index 8dfb865ecc81..a12cb14a04d4 100644 --- a/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java @@ -256,6 +256,11 @@ public abstract class SharedK2LocalInspectionTestGenerated extends AbstractShare runTest("../testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt"); } + @TestMetadata("inAnonymousFunction2.kt") + public void testInAnonymousFunction2() throws Exception { + runTest("../testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction2.kt"); + } + @TestMetadata("inFunction.kt") public void testInFunction() throws Exception { runTest("../testData/inspectionsLocal/redundantReturnLabel/inFunction.kt"); diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt index d714bfd763e0..a35245a920d2 100644 --- a/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt +++ b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt @@ -1,4 +1,3 @@ -// PROBLEM: none fun foo(f: (String?) -> Int) {} fun test() { diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt.after b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt.after new file mode 100644 index 000000000000..b29ae3ff98f5 --- /dev/null +++ b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt.after @@ -0,0 +1,8 @@ +fun foo(f: (String?) -> Int) {} + +fun test() { + foo(fun(it: String?): Int { + if (it != null) return 1 + return 0 + }) +} \ No newline at end of file diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction2.kt b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction2.kt new file mode 100644 index 000000000000..3a37bc27e664 --- /dev/null +++ b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction2.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +inline fun foo(f: (String?) -> Int): Int = f("") + +fun test(): Int { + return foo(fun(it: String?): Int { + if (it != null) return@test 1 + return 0 + }) +} \ No newline at end of file