KT-11710 "Replace 'if' with elvis operator": incorrect code generated for 'if' expression

#KT-11710 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-04-07 13:22:19 +03:00
parent 6a65442095
commit cdab9d5103
4 changed files with 18 additions and 1 deletions
@@ -475,7 +475,7 @@ public class KtPsiUtil {
while (!(current instanceof KtBlockExpression || current instanceof KtDeclaration || current instanceof KtStatementExpression)) {
if (current.getTextRange().getEndOffset() != currentInner.getTextRange().getEndOffset()) {
return current.getText().charAt(current.getTextLength() - 1) != ')'; // if current expression is "guarded" by parenthesis, no extra parenthesis is necessary
return !(current instanceof KtParenthesizedExpression) && !(current instanceof KtValueArgumentList); // if current expression is "guarded" by parenthesis, no extra parenthesis is necessary
}
current = current.getParent();
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(a: String?, b: String): String {
val x = if (true) a else b
<caret>if (x == null) throw Exception()
return x
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun test(a: String?, b: String): String {
val x = (if (true) a else b) ?: throw Exception()
return x
}
@@ -5364,6 +5364,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("ifStatementPriority.kt")
public void testIfStatementPriority() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/ifNullToElvis/ifStatementPriority.kt");
doTest(fileName);
}
@TestMetadata("MultiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/ifNullToElvis/MultiStatementBlock.kt");