Replace with safe call works now on UNSAFE_IMPLICIT_INVOKE #KT-12628 Fixed
(cherry picked from commit 208798d)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
a9c30e878b
commit
995a633229
@@ -205,6 +205,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
UNNECESSARY_NOT_NULL_ASSERTION.registerFactory(RemoveExclExclCallFix)
|
||||
UNSAFE_INFIX_CALL.registerFactory(ReplaceInfixCallFix)
|
||||
UNSAFE_CALL.registerFactory(ReplaceInfixCallFix) // [] only
|
||||
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(ReplaceInfixCallFix)
|
||||
|
||||
AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix())
|
||||
PROPERTY_WITH_NO_TYPE_NO_INITIALIZER.registerActions(SpecifyTypeExplicitlyFix())
|
||||
|
||||
@@ -51,6 +51,11 @@ class ReplaceInfixCallFix(element: KtExpression) : KotlinQuickFixAction<KtExpres
|
||||
element.replace(newExpression)
|
||||
}
|
||||
}
|
||||
is KtCallExpression -> {
|
||||
val newExpression = psiFactory.createExpressionByPattern(
|
||||
"$0?.invoke($1)", element.calleeExpression ?: return, element.valueArguments.joinToString(", ") { it.text })
|
||||
element.replace(newExpression)
|
||||
}
|
||||
is KtBinaryExpression -> {
|
||||
if (element.operationToken == KtTokens.IDENTIFIER) {
|
||||
val newExpression = psiFactory.createExpressionByPattern(
|
||||
@@ -78,9 +83,19 @@ class ReplaceInfixCallFix(element: KtExpression) : KotlinQuickFixAction<KtExpres
|
||||
if (expression.arrayExpression == null) return null
|
||||
return ReplaceInfixCallFix(expression)
|
||||
}
|
||||
val binaryExpression = expression.parent as? KtBinaryExpression ?: return null
|
||||
if (binaryExpression.left == null || binaryExpression.right == null) return null
|
||||
return ReplaceInfixCallFix(binaryExpression)
|
||||
val parent = expression.parent
|
||||
return when (parent) {
|
||||
is KtBinaryExpression -> {
|
||||
if (parent.left == null || parent.right == null) null
|
||||
else ReplaceInfixCallFix(parent)
|
||||
}
|
||||
is KtCallExpression -> {
|
||||
if (parent.calleeExpression == null) null
|
||||
else if (parent.parent is KtQualifiedExpression) null
|
||||
else ReplaceInfixCallFix(parent)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Replace with safe (?.) call" "true"
|
||||
|
||||
fun foo(exec: (() -> Unit)?) = exec<caret>()
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Replace with safe (?.) call" "true"
|
||||
|
||||
fun foo(exec: (() -> Unit)?) = exec?.invoke()
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Replace with safe (?.) call" "false"
|
||||
// ACTION: Convert to block body
|
||||
// ACTION: Replace overloaded operator with function call
|
||||
// ACTION: Wrap with '?.let { ... }' call
|
||||
// ERROR: Reference has a nullable type '(String.() -> Unit)?', use explicit '?.invoke()' to make a function-like call instead
|
||||
|
||||
fun foo(exec: (String.() -> Unit)?) = "".exec<caret>()
|
||||
@@ -6096,6 +6096,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unsafeInvoke.kt")
|
||||
public void testUnsafeInvoke() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvoke.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unsafeInvokeWithReceiver.kt")
|
||||
public void testUnsafeInvokeWithReceiver() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInvokeWithReceiver.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unsafePlus.kt")
|
||||
public void testUnsafePlus() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/unsafePlus.kt");
|
||||
|
||||
Reference in New Issue
Block a user