Wrap with safe let call handles qualified calls correctly #KT-13262 Fixed

(cherry picked from commit 7044348)
This commit is contained in:
Mikhail Glukhikh
2016-07-28 12:13:46 +03:00
committed by Mikhail Glukhikh
parent 77b7648f10
commit b7fd41844d
7 changed files with 47 additions and 4 deletions
@@ -365,5 +365,5 @@ fun <E : PsiElement> E.createSmartPointer(): SmartPsiElementPointer<E> =
fun PsiElement.before(element: PsiElement) = textRange.endOffset <= element.textRange.startOffset
inline fun <reified T : PsiElement> PsiElement.getLastParentOfTypeInRow() = parents.takeWhile { it is T }.lastOrNull()
inline fun <reified T : PsiElement> PsiElement.getLastParentOfTypeInRow() = parents.takeWhile { it is T }.lastOrNull() as? T
@@ -24,10 +24,9 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.isNullabilityMismatch
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
class WrapWithSafeLetCallFix(
expression: KtExpression,
@@ -71,7 +70,7 @@ class WrapWithSafeLetCallFix(
if (!isNullabilityMismatch(expected = typeMismatch.a, actual = typeMismatch.b)) return null
return WrapWithSafeLetCallFix(call, typeMismatch.psiElement)
return WrapWithSafeLetCallFix(call.getLastParentOfTypeInRow<KtQualifiedExpression>() ?: call, typeMismatch.psiElement)
}
}
}
@@ -0,0 +1,8 @@
// "Wrap with '?.let { ... }' call" "true"
// WITH_RUNTIME
fun Int.foo(x: Int) = this + x
val arg: Int? = 42
val res = 24.hashCode().foo(<caret>arg)
@@ -0,0 +1,8 @@
// "Wrap with '?.let { ... }' call" "true"
// WITH_RUNTIME
fun Int.foo(x: Int) = this + x
val arg: Int? = 42
val res = arg?.let { 24.hashCode().foo(it) }
@@ -0,0 +1,8 @@
// "Wrap with '?.let { ... }' call" "true"
// WITH_RUNTIME
object Obj {
fun foo(x: Int) = x
}
val arg: Int? = null
val argFoo = Obj.foo(<caret>arg)
@@ -0,0 +1,8 @@
// "Wrap with '?.let { ... }' call" "true"
// WITH_RUNTIME
object Obj {
fun foo(x: Int) = x
}
val arg: Int? = null
val argFoo = arg?.let { Obj.foo(it) }
@@ -9151,6 +9151,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("chainedCallTypeMismatch.kt")
public void testChainedCallTypeMismatch() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/chainedCallTypeMismatch.kt");
doTest(fileName);
}
@TestMetadata("chainedUnsafeCall.kt")
public void testChainedUnsafeCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/chainedUnsafeCall.kt");
@@ -9181,6 +9187,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("objectQualifier.kt")
public void testObjectQualifier() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/objectQualifier.kt");
doTest(fileName);
}
@TestMetadata("unstableValue.kt")
public void testUnstableValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/unstableValue.kt");