"Add not-null asserted (!!) call": do not add this when implicit receiver is not extension receiver
#KT-34894 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
f1c605d0f7
commit
8857827dce
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
|||||||
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceiverValue
|
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceiverValue
|
||||||
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
|
||||||
import org.jetbrains.kotlin.types.TypeUtils
|
import org.jetbrains.kotlin.types.TypeUtils
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||||
@@ -130,7 +131,7 @@ class AddExclExclCallFix(psiElement: PsiElement, val checkImplicitReceivers: Boo
|
|||||||
}
|
}
|
||||||
is KtExpression -> {
|
is KtExpression -> {
|
||||||
val context = psiElement.analyze()
|
val context = psiElement.analyze()
|
||||||
if (checkImplicitReceivers && psiElement.getResolvedCall(context)?.getImplicitReceiverValue() != null) {
|
if (checkImplicitReceivers && psiElement.getResolvedCall(context)?.getImplicitReceiverValue() is ExtensionReceiver) {
|
||||||
val expressionToReplace = psiElement.parent as? KtCallExpression ?: psiElement
|
val expressionToReplace = psiElement.parent as? KtCallExpression ?: psiElement
|
||||||
expressionToReplace.expressionForCall(implicitReceiver = true)
|
expressionToReplace.expressionForCall(implicitReceiver = true)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add non-null asserted (!!) call" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test(s: String?) {
|
||||||
|
s.run {
|
||||||
|
<caret>length
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add non-null asserted (!!) call" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
fun test(s: String?) {
|
||||||
|
s.run {
|
||||||
|
this!!.length
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// "Add non-null asserted (!!) call" "true"
|
||||||
|
class Foo {
|
||||||
|
val project: Project? = null
|
||||||
|
|
||||||
|
fun quux() {
|
||||||
|
baz(<caret>project)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun baz(project: Project) {}
|
||||||
|
|
||||||
|
class Project
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// "Add non-null asserted (!!) call" "true"
|
||||||
|
class Foo {
|
||||||
|
val project: Project? = null
|
||||||
|
|
||||||
|
fun quux() {
|
||||||
|
baz(project!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun baz(project: Project) {}
|
||||||
|
|
||||||
|
class Project
|
||||||
|
}
|
||||||
@@ -598,6 +598,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
runTest("idea/testData/quickfix/addExclExclCall/implicit.kt");
|
runTest("idea/testData/quickfix/addExclExclCall/implicit.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("implicit2.kt")
|
||||||
|
public void testImplicit2() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/addExclExclCall/implicit2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("implicit3.kt")
|
||||||
|
public void testImplicit3() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/addExclExclCall/implicit3.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("implicitFunctionCall.kt")
|
@TestMetadata("implicitFunctionCall.kt")
|
||||||
public void testImplicitFunctionCall() throws Exception {
|
public void testImplicitFunctionCall() throws Exception {
|
||||||
runTest("idea/testData/quickfix/addExclExclCall/implicitFunctionCall.kt");
|
runTest("idea/testData/quickfix/addExclExclCall/implicitFunctionCall.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user