KT-13262 related: surround with null check is applicable now also for qualified calls with argument type mismatch
(cherry picked from commit 8f13c84)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b7fd41844d
commit
39090113e9
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||
@@ -93,13 +94,15 @@ class SurroundWithNullCheckFix(
|
||||
val nullableExpression = typeMismatch.psiElement as? KtReferenceExpression ?: return null
|
||||
val argument = nullableExpression.parent as? KtValueArgument ?: return null
|
||||
val call = argument.getParentOfType<KtCallExpression>(true) ?: return null
|
||||
if (call.parent !is KtBlockExpression) return null
|
||||
|
||||
val rootCall = call.getLastParentOfTypeInRow<KtQualifiedExpression>() ?: call
|
||||
if (rootCall.parent !is KtBlockExpression) return null
|
||||
|
||||
if (!isNullabilityMismatch(expected = typeMismatch.a, actual = typeMismatch.b)) return null
|
||||
|
||||
if (!nullableExpression.isStable()) return null
|
||||
|
||||
return SurroundWithNullCheckFix(call, nullableExpression)
|
||||
return SurroundWithNullCheckFix(rootCall, nullableExpression)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Surround with null check" "true"
|
||||
|
||||
object Obj {
|
||||
fun foo(x: Int) = x
|
||||
}
|
||||
|
||||
fun use(arg: Int?) {
|
||||
Obj.foo(<caret>arg)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Surround with null check" "true"
|
||||
|
||||
object Obj {
|
||||
fun foo(x: Int) = x
|
||||
}
|
||||
|
||||
fun use(arg: Int?) {
|
||||
if (arg != null) {
|
||||
Obj.foo(arg)
|
||||
}
|
||||
}
|
||||
@@ -7756,6 +7756,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectQualifier.kt")
|
||||
public void testObjectQualifier() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/objectQualifier.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simpleUnsafeCall.kt")
|
||||
public void testSimpleUnsafeCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/simpleUnsafeCall.kt");
|
||||
|
||||
Reference in New Issue
Block a user