diff --git a/idea/resources/inspectionDescriptions/RedundantIf.html b/idea/resources/inspectionDescriptions/RedundantIf.html
index 33098205189..de309fd62cb 100644
--- a/idea/resources/inspectionDescriptions/RedundantIf.html
+++ b/idea/resources/inspectionDescriptions/RedundantIf.html
@@ -1,6 +1,6 @@
-Reports if statements which can be simplified to single return statements.
+Reports if statements which can be simplified to single statements.
For example:
diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt
index 30c91a74404..90ebe46cf29 100644
--- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt
+++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantIfInspection.kt
@@ -20,6 +20,7 @@ import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.intentions.negate
+import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
@@ -29,66 +30,81 @@ class RedundantIfInspection : AbstractKotlinInspection(), CleanupLocalInspection
override fun visitIfExpression(expression: KtIfExpression) {
super.visitIfExpression(expression)
if (expression.condition == null) return
- val type = RedundantType.of(expression)
- if (type == RedundantType.NONE) return
+ val (redundancyType, branchType) = RedundancyType.of(expression)
+ if (redundancyType == RedundancyType.NONE) return
+
holder.registerProblem(expression,
"Redundant 'if' statement",
ProblemHighlightType.WEAK_WARNING,
- RemoveRedundantIf(type))
+ RemoveRedundantIf(redundancyType, branchType))
}
}
}
- private enum class RedundantType {
+ private sealed class BranchType {
+ object Simple : BranchType()
+
+ object Return : BranchType()
+
+ class Assign(val lvalue: KtExpression) : BranchType() {
+ override fun equals(other: Any?) = other is Assign && lvalue.text == other.lvalue.text
+
+ override fun hashCode() = lvalue.text.hashCode()
+ }
+ }
+
+ private enum class RedundancyType {
NONE,
THEN_TRUE,
ELSE_TRUE;
companion object {
- internal fun of(expression: KtIfExpression): RedundantType {
- val thenReturn = getReturnedExpression(expression.then) ?: return NONE
- val elseReturn = getReturnedExpression(expression.`else`) ?: return NONE
+ internal fun of(expression: KtIfExpression): Pair {
+ val (thenReturn, thenType) = expression.then.getBranchExpression() ?: return NONE to BranchType.Simple
+ val (elseReturn, elseType) = expression.`else`.getBranchExpression() ?: return NONE to BranchType.Simple
- return if (KtPsiUtil.isTrueConstant(thenReturn) && KtPsiUtil.isFalseConstant(elseReturn)) {
- THEN_TRUE
- }
- else if (KtPsiUtil.isFalseConstant(thenReturn) && KtPsiUtil.isTrueConstant(elseReturn)) {
- ELSE_TRUE
- }
- else {
- NONE
+ return when {
+ thenType != elseType -> NONE to BranchType.Simple
+ KtPsiUtil.isTrueConstant(thenReturn) && KtPsiUtil.isFalseConstant(elseReturn) -> THEN_TRUE to thenType
+ KtPsiUtil.isFalseConstant(thenReturn) && KtPsiUtil.isTrueConstant(elseReturn) -> ELSE_TRUE to thenType
+ else -> NONE to BranchType.Simple
}
}
- private fun getReturnedExpression(expression: KtExpression?) : KtExpression? {
- return when (expression) {
- is KtReturnExpression -> {
- expression.returnedExpression
- }
- is KtBlockExpression -> {
- val statement = expression.statements.singleOrNull() as? KtReturnExpression ?: return null
- statement.returnedExpression
- }
- else -> {
+ private fun KtExpression?.getBranchExpression(): Pair? {
+ return when (this) {
+ is KtReturnExpression -> returnedExpression to BranchType.Return
+ is KtBlockExpression -> statements.singleOrNull()?.getBranchExpression()
+ is KtBinaryExpression -> if (operationToken == KtTokens.EQ && left != null)
+ right to BranchType.Assign(left!!)
+ else
null
- }
+ is KtExpression -> this to BranchType.Simple
+ else -> null
}
}
}
}
- private class RemoveRedundantIf(private val redundantType: RedundantType) : LocalQuickFix {
+ private class RemoveRedundantIf(private val redundancyType: RedundancyType, private val branchType: BranchType) : LocalQuickFix {
override fun getName() = "Remove redundant 'if' statement"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement as KtIfExpression
- val condition = when (redundantType) {
- RedundantType.NONE -> return
- RedundantType.THEN_TRUE -> element.condition!!
- RedundantType.ELSE_TRUE -> element.condition!!.negate()
+ val condition = when (redundancyType) {
+ RedundancyType.NONE -> return
+ RedundancyType.THEN_TRUE -> element.condition!!
+ RedundancyType.ELSE_TRUE -> element.condition!!.negate()
}
- element.replace(KtPsiFactory(element).createExpressionByPattern("return $0", condition.text))
+ 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
+ }
+ )
}
}
diff --git a/idea/testData/quickfix/redundantIf/assignExpression.kt b/idea/testData/quickfix/redundantIf/assignExpression.kt
new file mode 100644
index 00000000000..8f38dcf4569
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/assignExpression.kt
@@ -0,0 +1,4 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(p: Int) {
+ val v1 = if (p > 0) true else false
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/assignExpression.kt.after b/idea/testData/quickfix/redundantIf/assignExpression.kt.after
new file mode 100644
index 00000000000..1a178c8f3fa
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/assignExpression.kt.after
@@ -0,0 +1,4 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(p: Int) {
+ val v1 = p > 0
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/assignment.kt b/idea/testData/quickfix/redundantIf/assignment.kt
new file mode 100644
index 00000000000..1077eb71c76
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/assignment.kt
@@ -0,0 +1,10 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(p: Int) {
+ var v2 = false
+ if (p > 0) {
+ v2 = false
+ }
+ else {
+ v2 = true
+ }
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/assignment.kt.after b/idea/testData/quickfix/redundantIf/assignment.kt.after
new file mode 100644
index 00000000000..a2a4af967e3
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/assignment.kt.after
@@ -0,0 +1,5 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(p: Int) {
+ var v2 = false
+ v2 = p <= 0
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/assignmentFake.kt b/idea/testData/quickfix/redundantIf/assignmentFake.kt
new file mode 100644
index 00000000000..2fa20549912
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/assignmentFake.kt
@@ -0,0 +1,10 @@
+// "Remove redundant 'if' statement" "false"
+// ACTION: Add braces to 'if' statement
+// ACTION: Invert 'if' condition
+// ACTION: Replace 'if' with 'when'
+
+fun bar(p: Int) {
+ var v1 = false
+ var v2 = false
+ if (p > 0) v2 = true else v1 = false
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/expression.kt b/idea/testData/quickfix/redundantIf/expression.kt
new file mode 100644
index 00000000000..a2dc7bb1ce0
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/expression.kt
@@ -0,0 +1,5 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(value: Int): Boolean {
+ val x = if (value % 2 == 0) false else true
+ return x
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/expression.kt.after b/idea/testData/quickfix/redundantIf/expression.kt.after
new file mode 100644
index 00000000000..1d4c6bede33
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/expression.kt.after
@@ -0,0 +1,5 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(value: Int): Boolean {
+ val x = value % 2 != 0
+ return x
+}
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/expressionBody.kt b/idea/testData/quickfix/redundantIf/expressionBody.kt
new file mode 100644
index 00000000000..15a0ef95576
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/expressionBody.kt
@@ -0,0 +1,2 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(value: Int) = if (value % 2 == 0) true else false
\ No newline at end of file
diff --git a/idea/testData/quickfix/redundantIf/expressionBody.kt.after b/idea/testData/quickfix/redundantIf/expressionBody.kt.after
new file mode 100644
index 00000000000..6934ef162f1
--- /dev/null
+++ b/idea/testData/quickfix/redundantIf/expressionBody.kt.after
@@ -0,0 +1,2 @@
+// "Remove redundant 'if' statement" "true"
+fun bar(value: Int) = value % 2 == 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 e3eb43a7e02..d88feabb5d0 100644
--- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java
+++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java
@@ -7173,6 +7173,36 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/redundantIf"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
+ @TestMetadata("assignExpression.kt")
+ public void testAssignExpression() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/assignExpression.kt");
+ doTest(fileName);
+ }
+
+ @TestMetadata("assignment.kt")
+ public void testAssignment() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/assignment.kt");
+ doTest(fileName);
+ }
+
+ @TestMetadata("assignmentFake.kt")
+ public void testAssignmentFake() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/assignmentFake.kt");
+ doTest(fileName);
+ }
+
+ @TestMetadata("expression.kt")
+ public void testExpression() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/expression.kt");
+ doTest(fileName);
+ }
+
+ @TestMetadata("expressionBody.kt")
+ public void testExpressionBody() throws Exception {
+ String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/expressionBody.kt");
+ doTest(fileName);
+ }
+
@TestMetadata("negate.kt")
public void testNegate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/redundantIf/negate.kt");