KT-4008 'Replace if with when' is available but does not work
This commit is contained in:
+5
-6
@@ -20,11 +20,11 @@ public class IfWhenUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) {
|
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) {
|
public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) {
|
||||||
return !whenExpression.getEntries().isEmpty() && JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression);
|
return !whenExpression.getEntries().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertNotNull(JetExpression expression) {
|
private static void assertNotNull(JetExpression expression) {
|
||||||
@@ -85,7 +85,6 @@ public class IfWhenUtils {
|
|||||||
JetExpression elseBranch = currIfExpression.getElse();
|
JetExpression elseBranch = currIfExpression.getElse();
|
||||||
|
|
||||||
assertNotNull(thenBranch);
|
assertNotNull(thenBranch);
|
||||||
assertNotNull(elseBranch);
|
|
||||||
|
|
||||||
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
|
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
|
||||||
|
|
||||||
@@ -97,7 +96,6 @@ public class IfWhenUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.branchExpression(thenBranch);
|
builder.branchExpression(thenBranch);
|
||||||
|
|
||||||
if (elseBranch instanceof JetIfExpression) {
|
if (elseBranch instanceof JetIfExpression) {
|
||||||
@@ -105,8 +103,9 @@ public class IfWhenUtils {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
currIfExpression = null;
|
currIfExpression = null;
|
||||||
//noinspection ConstantConditions
|
if (elseBranch != null) {
|
||||||
builder.elseEntry(elseBranch);
|
builder.elseEntry(elseBranch);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} while (currIfExpression != null);
|
} 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
|
||||||
// ERROR: Unreachable code
|
// ERROR: Unreachable code
|
||||||
// ACTION: Disable 'Eliminate Argument of 'when''
|
// ACTION: Disable 'Eliminate Argument of 'when''
|
||||||
|
// ACTION: Disable 'Replace 'when' with 'if''
|
||||||
|
// ACTION: Edit intention settings
|
||||||
// ACTION: Edit intention settings
|
// ACTION: Edit intention settings
|
||||||
// ACTION: Eliminate argument of 'when'
|
// ACTION: Eliminate argument of 'when'
|
||||||
|
// ACTION: Replace 'when' with 'if'
|
||||||
package foo
|
package foo
|
||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
|
|||||||
@@ -451,6 +451,11 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
|||||||
doTestIfToWhen("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithRangeTestsAndUnparenthesizedMultiConditions.kt");
|
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")
|
@TestMetadata("whenWithMultipleConditionTypes.kt")
|
||||||
public void testWhenWithMultipleConditionTypes() throws Exception {
|
public void testWhenWithMultipleConditionTypes() throws Exception {
|
||||||
doTestIfToWhen("idea/testData/intentions/branched/ifWhen/ifToWhen/whenWithMultipleConditionTypes.kt");
|
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");
|
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")
|
@TestMetadata("whenWithoutSubject.kt")
|
||||||
public void testWhenWithoutSubject() throws Exception {
|
public void testWhenWithoutSubject() throws Exception {
|
||||||
doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutSubject.kt");
|
doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithoutSubject.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user