From b0fda5f451599c6fb6b7b9051e7b73a32656c556 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 7 Jul 2017 17:37:22 +0300 Subject: [PATCH] Lift return out fix: do not suggest for labeled returns --- .../branchedTransformations/BranchedFoldingUtils.kt | 4 +++- .../liftOut/whenToReturn/localReturns.kt | 13 +++++++++++++ .../inspections/LocalInspectionTestGenerated.java | 6 ++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspectionsLocal/liftOut/whenToReturn/localReturns.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt index 9aa0b3be5c4..dc13095d2fd 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/BranchedFoldingUtils.kt @@ -45,7 +45,9 @@ object BranchedFoldingUtils { fun getFoldableBranchedReturn(branch: KtExpression?): KtReturnExpression? = (branch?.lastBlockStatementOrThis() as? KtReturnExpression)?.takeIf { - it.returnedExpression != null && it.returnedExpression !is KtLambdaExpression + it.returnedExpression != null && + it.returnedExpression !is KtLambdaExpression && + it.getTargetLabel() == null } fun checkAssignmentsMatch(a1: KtBinaryExpression, a2: KtBinaryExpression): Boolean = diff --git a/idea/testData/inspectionsLocal/liftOut/whenToReturn/localReturns.kt b/idea/testData/inspectionsLocal/liftOut/whenToReturn/localReturns.kt new file mode 100644 index 00000000000..db4d40ac381 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/whenToReturn/localReturns.kt @@ -0,0 +1,13 @@ +// PROBLEM: none +// WITH_RUNTIME + +fun test(arg: String?): Int { + arg?.let { + when (arg) { + "" -> return 1 + else -> return@let 42 + } + 42 + } + return 0 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index c0b7c21f3a2..1f25bc0bae9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -712,6 +712,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } + @TestMetadata("localReturns.kt") + public void testLocalReturns() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/whenToReturn/localReturns.kt"); + doTest(fileName); + } + @TestMetadata("otherReturns.kt") public void testOtherReturns() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/whenToReturn/otherReturns.kt");