KT-4008 'Replace if with when' is available but does not work

This commit is contained in:
Alexey Sedunov
2013-10-11 13:57:00 +04:00
parent e05e616158
commit eff62bfd83
7 changed files with 53 additions and 6 deletions
@@ -20,11 +20,11 @@ public class IfWhenUtils {
}
public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) {
return ifExpression.getThen() != null && ifExpression.getElse() != null;
return ifExpression.getThen() != null;
}
public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) {
return !whenExpression.getEntries().isEmpty() && JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression);
return !whenExpression.getEntries().isEmpty();
}
private static void assertNotNull(JetExpression expression) {
@@ -85,7 +85,6 @@ public class IfWhenUtils {
JetExpression elseBranch = currIfExpression.getElse();
assertNotNull(thenBranch);
assertNotNull(elseBranch);
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
@@ -97,7 +96,6 @@ public class IfWhenUtils {
}
}
//noinspection ConstantConditions
builder.branchExpression(thenBranch);
if (elseBranch instanceof JetIfExpression) {
@@ -105,8 +103,9 @@ public class IfWhenUtils {
}
else {
currIfExpression = null;
//noinspection ConstantConditions
builder.elseEntry(elseBranch);
if (elseBranch != null) {
builder.elseEntry(elseBranch);
}
}
} while (currIfExpression != null);
@@ -0,0 +1,10 @@
fun test(n: Int) {
val s = "test"
<caret>if (n == 0)
s = "zero"
else if (n == 1)
s = "one"
else if (n == 2)
s = "two"
return s
}
@@ -0,0 +1,9 @@
fun test(n: Int) {
val s = "test"
when (n) {
0 -> s = "zero"
1 -> s = "one"
2 -> s = "two"
}
return s
}
@@ -0,0 +1,9 @@
fun test(n: Int) {
val s = "test"
<caret>when (n) {
0 -> s = "zero"
1 -> s = "one"
2 -> s = "two"
}
return s
}
@@ -0,0 +1,7 @@
fun test(n: Int) {
val s = "test"
if (n == 0) s = "zero"
else if (n == 1) s = "one"
else if (n == 2) s = "two"
return s
}
@@ -4,8 +4,11 @@
// ERROR: Unreachable code
// ERROR: Unreachable code
// ACTION: Disable 'Eliminate Argument of 'when''
// ACTION: Disable 'Replace 'when' with 'if''
// ACTION: Edit intention settings
// ACTION: Edit intention settings
// ACTION: Eliminate argument of 'when'
// ACTION: Replace 'when' with 'if'
package foo
fun foo() {
@@ -451,6 +451,11 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
doTestIfToWhen("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithRangeTestsAndUnparenthesizedMultiConditions.kt");
}
@TestMetadata("ifWithoutElse.kt")
public void testIfWithoutElse() throws Exception {
doTestIfToWhen("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithoutElse.kt");
}
@TestMetadata("whenWithMultipleConditionTypes.kt")
public void testWhenWithMultipleConditionTypes() throws Exception {
doTestIfToWhen("idea/testData/intentions/branched/ifWhen/ifToWhen/whenWithMultipleConditionTypes.kt");
@@ -504,6 +509,11 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithRangeTestsAndMultiConditions.kt");
}
@TestMetadata("whenWithoutElse.kt")
public void testWhenWithoutElse() throws Exception {
doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutElse.kt");
}
@TestMetadata("whenWithoutSubject.kt")
public void testWhenWithoutSubject() throws Exception {
doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutSubject.kt");