Partial body resolve correctly handles elvis operator

This commit is contained in:
Valentin Kipyatkov
2015-03-24 22:35:18 +03:00
parent 176ba937ba
commit f3799a0ce7
6 changed files with 46 additions and 2 deletions
@@ -157,8 +157,9 @@ class PartialBodyResolveFilter(
}
fun addPlaces(name: SmartCastName, places: Collection<JetExpression>) {
assert(!places.isEmpty())
map.getOrPut(name, { ArrayList(places.size()) }).addAll(places)
if (places.isNotEmpty()) {
map.getOrPut(name, { ArrayList(places.size()) }).addAll(places)
}
}
fun addIfCanBeSmartCast(expression: JetExpression) {
@@ -185,6 +186,22 @@ class PartialBodyResolveFilter(
}
}
override fun visitBinaryExpression(expression: JetBinaryExpression) {
expression.acceptChildren(this)
if (expression.getOperationToken() == JetTokens.ELVIS) {
val left = expression.getLeft()
val right = expression.getRight()
if (left != null && right != null) {
val smartCastName = left.smartCastExpressionName()
if (smartCastName != null && filter(smartCastName)) {
val exits = collectAlwaysExitPoints(right)
addPlaces(smartCastName, exits)
}
}
}
}
override fun visitIfExpression(expression: JetIfExpression) {
val condition = expression.getCondition()
val thenBranch = expression.getThen()
@@ -0,0 +1,6 @@
Resolve target: value-parameter val p1: kotlin.Any?
----------------------------------------------
fun foo(p1: Any?, p2: Any) {
/* STATEMENT DELETED: print(p1 ?: p2) */
<caret>p1?.hashCode()
}
@@ -0,0 +1,4 @@
fun foo(p1: Any?, p2: Any) {
print(p1 ?: p2)
<caret>p1?.hashCode()
}
@@ -0,0 +1,6 @@
Resolve target: value-parameter val p: kotlin.Any? smart-cast to kotlin.Any
----------------------------------------------
fun foo(p: Any?) {
print(p ?: return)
<caret>p.hashCode()
}
@@ -0,0 +1,4 @@
fun foo(p: Any?) {
print(p ?: return)
<caret>p.hashCode()
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea.resolve;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.InnerTestClasses;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
@@ -77,6 +78,12 @@ public class PartialBodyResolveTestGenerated extends AbstractPartialBodyResolveT
doTest(fileName);
}
@TestMetadata("Elvis.kt")
public void testElvis() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/Elvis.kt");
doTest(fileName);
}
@TestMetadata("ElvisReturn.kt")
public void testElvisReturn() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/ElvisReturn.kt");