From 3b147864f10f545f6f89ef0fbfd16a05cffc9dac Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sat, 20 May 2023 11:51:54 +0900 Subject: [PATCH] [kotlin] Apply RedundantReturnLabelInspection for anonymous functions ^KTIJ-24241 Fixed closes https://github.com/JetBrains/intellij-community/pull/2445 GitOrigin-RevId: 56b0c2b01af8f24c1f04c259585573f000e1a162 --- .../inspections/shared/RedundantReturnLabelInspection.kt | 7 ++++++- .../shared/SharedK1LocalInspectionTestGenerated.java | 5 +++++ .../shared/SharedK2LocalInspectionTestGenerated.java | 5 +++++ .../redundantReturnLabel/inAnonymousFunction.kt | 1 - .../redundantReturnLabel/inAnonymousFunction.kt.after | 8 ++++++++ .../redundantReturnLabel/inAnonymousFunction2.kt | 9 +++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction.kt.after create mode 100644 plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/redundantReturnLabel/inAnonymousFunction2.kt 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