diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt index 08e6f82ca5e..48cae872f83 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt @@ -20,6 +20,7 @@ import com.intellij.codeInspection.* import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils +import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isIfBranch import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtIfExpression @@ -33,6 +34,8 @@ class LiftReturnOrAssignmentInspection : AbstractKotlinInspection() { object : KtVisitorVoid() { private fun visitIfOrWhen(expression: KtExpression, keyword: PsiElement) { if (expression.lineCount() > LINES_LIMIT) return + if (expression.isIfBranch()) return + val returnNumber = BranchedFoldingUtils.getFoldableReturnNumber(expression) if (returnNumber > 0) { holder.registerProblem( diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index 059b94e2212..4ce7a3f8182 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -264,3 +264,11 @@ internal fun KtExpression.lineCount(): Int { } internal fun KtExpression.isOneLiner(): Boolean = lineCount() == 1 + +internal fun KtExpression.isIfBranch(): Boolean { + if (parent is KtContainerNodeForControlStructureBody) { + val grandParent = parent.parent + if (grandParent is KtIfExpression && grandParent.`else` == this) return true + } + return false +} diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/elseIfOnly.kt b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/elseIfOnly.kt new file mode 100644 index 00000000000..34bf2d034e8 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/elseIfOnly.kt @@ -0,0 +1,15 @@ +// PROBLEM: none + +fun foo(a: Boolean, b: Boolean): String { + var res = "" + if (a) { + + } + else if (b) { + res += "b" + } + else { + res += "!b" + } + return res +} \ 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 cf74d41981e..8f1c834bfed 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -418,6 +418,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { doTest(fileName); } + @TestMetadata("elseIfOnly.kt") + public void testElseIfOnly() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/elseIfOnly.kt"); + doTest(fileName); + } + @TestMetadata("ifElseIf.kt") public void testIfElseIf() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/liftOut/ifToAssignment/ifElseIf.kt");