More accurate handling of Elvis in areParenthesesNecessary #KT-15227 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-12-19 21:01:39 +03:00
parent 6d6a16f399
commit ee1b741e84
5 changed files with 36 additions and 1 deletions
@@ -503,7 +503,10 @@ public class KtPsiUtil {
if (parentElement instanceof KtLabeledExpression) return false;
// 'x ?: ...' case
if (parentElement instanceof KtBinaryExpression && parentOperation == KtTokens.ELVIS && currentInner == ((KtBinaryExpression) parentElement).getRight()) {
if (parentElement instanceof KtBinaryExpression &&
parentOperation == KtTokens.ELVIS &&
!(innerExpression instanceof KtBinaryExpression) &&
currentInner == ((KtBinaryExpression) parentElement).getRight()) {
return false;
}
@@ -0,0 +1,9 @@
// See KT-15227
class My(val local: Boolean)
class Your(val my: My?, val parent: Any?)
fun foo(your: Your): Boolean {
val my = your.my
return <caret>if (my != null) my.local else your.parent != null
}
@@ -0,0 +1,9 @@
// See KT-15227
class My(val local: Boolean)
class Your(val my: My?, val parent: Any?)
fun foo(your: Your): Boolean {
val my = your.my
return my?.local ?: (your.parent != null)
}
@@ -87,4 +87,12 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>Replace 'if' expression with elvis expression</description>
</problem>
<problem>
<file>comparisonInElse.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/comparisonInElse.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?:'</problem_class>
<description>Replace 'if' expression with elvis expression</description>
</problem>
</problems>
@@ -1294,6 +1294,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("comparisonInElse.kt")
public void testComparisonInElse() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/comparisonInElse.kt");
doTest(fileName);
}
@TestMetadata("conditionComparesNullWithNull.kt")
public void testConditionComparesNullWithNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifThenToElvis/conditionComparesNullWithNull.kt");