"Replace if with when": do not add empty else block #KT-18681 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-06-13 12:18:22 +03:00
committed by Mikhail Glukhikh
parent 3e207fd6b2
commit 0ae2054af4
4 changed files with 24 additions and 1 deletions
@@ -63,7 +63,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
builder.append(nextSibling.text)
nextSibling = nextSibling.nextSibling ?: break
}
KtPsiFactory(ifExpression).createBlock(builder.toString())
KtPsiFactory(ifExpression).createBlock(builder.toString()).takeIf { it.statements.isNotEmpty() }
}
}
}
@@ -0,0 +1,8 @@
fun test(a: Any, b: Boolean): Int {
when (a) {
is Int -> {
<caret>if (b) return 1
}
}
return 0
}
@@ -0,0 +1,10 @@
fun test(a: Any, b: Boolean): Int {
when (a) {
is Int -> {
when {
b -> return 1
}
}
}
return 0
}
@@ -2514,6 +2514,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt");
}
@TestMetadata("ifThenReturn.kt")
public void testIfThenReturn() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifThenReturn.kt");
}
@TestMetadata("ifWithEqualityTests.kt")
public void testIfWithEqualityTests() throws Exception {
runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithEqualityTests.kt");