Skip to content

Commit c8e58ce

Browse files
shirajimglukhikh
authored andcommitted
Fix WrapWithSafeLetCallFix ignore extension method
Related to KT-17726
1 parent bd73916 commit c8e58ce

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

idea/src/org/jetbrains/kotlin/idea/quickfix/WrapWithSafeLetCallFix.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import com.intellij.openapi.editor.Editor
2121
import com.intellij.openapi.project.Project
2222
import org.jetbrains.kotlin.diagnostics.Diagnostic
2323
import org.jetbrains.kotlin.diagnostics.Errors
24+
import org.jetbrains.kotlin.idea.caches.resolve.analyze
2425
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
2526
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
2627
import org.jetbrains.kotlin.psi.*
2728
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
2829
import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow
2930
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
31+
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
3032
import org.jetbrains.kotlin.types.typeUtil.isNullabilityMismatch
3133

3234
class WrapWithSafeLetCallFix(
@@ -57,6 +59,12 @@ class WrapWithSafeLetCallFix(
5759
object UnsafeFactory : KotlinSingleIntentionActionFactory() {
5860
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
5961
val element = diagnostic.psiElement
62+
63+
if (element is KtNameReferenceExpression) {
64+
val resolvedCall = element.getResolvedCall(element.analyze())
65+
if (resolvedCall?.call?.callType != Call.CallType.INVOKE) return null
66+
}
67+
6068
val expression = element.getParentOfType<KtExpression>(true) ?: return null
6169

6270
val parent = element.parent

idea/testData/quickfix/replaceWithSafeCall/noReplaceWithSafeCallForImplicitReceiver.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// ACTION: Add non-null asserted (!!) call
33
// ACTION: Convert to expression body
44
// ACTION: Replace with safe (this?.) call
5-
// ACTION: Wrap with '?.let { ... }' call
65
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type A?
76

87
class A {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// "Wrap with '?.let { ... }' call" "false"
2+
// WITH_RUNTIME
3+
// ACTION: Add non-null asserted (!!) call
4+
// ACTION: Introduce local variable
5+
// ACTION: Replace with safe (this?.) call
6+
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
7+
8+
fun String?.foo() {
9+
toLowerCase<caret>()
10+
}

idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11243,6 +11243,12 @@ public void testExpressionUnsafeCall() throws Exception {
1124311243
doTest(fileName);
1124411244
}
1124511245

11246+
@TestMetadata("extensionMethod.kt")
11247+
public void testExtensionMethod() throws Exception {
11248+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/extensionMethod.kt");
11249+
doTest(fileName);
11250+
}
11251+
1124611252
@TestMetadata("insideLet.kt")
1124711253
public void testInsideLet() throws Exception {
1124811254
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/insideLet.kt");

0 commit comments

Comments
 (0)