diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt index d77bf07fa26..be3b99c6b94 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt @@ -21,10 +21,12 @@ class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspection val (redundancyType, branchType) = RedundancyType.of(expression) if (redundancyType == RedundancyType.NONE) return@ifExpressionVisitor - holder.registerProblem(expression, - "Redundant 'if' statement", - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, - RemoveRedundantIf(redundancyType, branchType)) + holder.registerProblem( + expression, + "Redundant 'if' statement", + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, + RemoveRedundantIf(redundancyType, branchType) + ) } } @@ -33,6 +35,8 @@ class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspection object Return : BranchType() + data class LabeledReturn(val label: String) : BranchType() + class Assign(val lvalue: KtExpression) : BranchType() { override fun equals(other: Any?) = other is Assign && lvalue.text == other.lvalue.text @@ -60,7 +64,10 @@ class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspection private fun KtExpression?.getBranchExpression(): Pair? { return when (this) { - is KtReturnExpression -> returnedExpression to BranchType.Return + is KtReturnExpression -> { + val branchType = labeledExpression?.let { BranchType.LabeledReturn(it.text) } ?: BranchType.Return + returnedExpression to branchType + } is KtBlockExpression -> statements.singleOrNull()?.getBranchExpression() is KtBinaryExpression -> if (operationToken == KtTokens.EQ && left != null) right to BranchType.Assign(left!!) @@ -87,11 +94,12 @@ class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspection } val factory = KtPsiFactory(element) element.replace( - when (branchType) { - is BranchType.Return -> factory.createExpressionByPattern("return $0", condition) - is BranchType.Assign -> factory.createExpressionByPattern("$0 = $1", branchType.lvalue, condition) - else -> condition - } + when (branchType) { + is BranchType.Return -> factory.createExpressionByPattern("return $0", condition) + is BranchType.LabeledReturn -> factory.createExpressionByPattern("return${branchType.label} $0", condition) + is BranchType.Assign -> factory.createExpressionByPattern("$0 = $1", branchType.lvalue, condition) + else -> condition + } ) } } diff --git a/idea/testData/quickfix/redundantIf/labeledReturn.kt b/idea/testData/quickfix/redundantIf/labeledReturn.kt new file mode 100644 index 00000000000..b917b1143c6 --- /dev/null +++ b/idea/testData/quickfix/redundantIf/labeledReturn.kt @@ -0,0 +1,11 @@ +// "Remove redundant 'if' statement" "true" +// WITH_RUNTIME +fun foo() { + listOf(1,2,3).find { + if (it > 0) { + return@find true + } else { + return@find false + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/redundantIf/labeledReturn.kt.after b/idea/testData/quickfix/redundantIf/labeledReturn.kt.after new file mode 100644 index 00000000000..8b22ede5893 --- /dev/null +++ b/idea/testData/quickfix/redundantIf/labeledReturn.kt.after @@ -0,0 +1,7 @@ +// "Remove redundant 'if' statement" "true" +// WITH_RUNTIME +fun foo() { + listOf(1,2,3).find { + return@find it > 0 + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index cfb4479d029..3e4c55a2a51 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -9126,6 +9126,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("labeledReturn.kt") + public void testLabeledReturn() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/labeledReturn.kt"); + doTest(fileName); + } + @TestMetadata("negate.kt") public void testNegate() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/negate.kt");