Quick-fix "wrap with safe let call" introduced #KT-11104 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
f8b6ed226a
commit
47c1106d5d
@@ -194,6 +194,10 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(SurroundWithNullCheckFix)
|
||||
UNSAFE_INFIX_CALL.registerFactory(SurroundWithNullCheckFix)
|
||||
|
||||
UNSAFE_CALL.registerFactory(WrapWithSafeLetCallFix)
|
||||
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(WrapWithSafeLetCallFix)
|
||||
UNSAFE_INFIX_CALL.registerFactory(WrapWithSafeLetCallFix)
|
||||
|
||||
UNSAFE_CALL.registerFactory(AddExclExclCallFix)
|
||||
UNNECESSARY_NOT_NULL_ASSERTION.registerFactory(RemoveExclExclCallFix)
|
||||
UNSAFE_INFIX_CALL.registerFactory(ReplaceInfixCallFix)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
|
||||
class WrapWithSafeLetCallFix(
|
||||
expression: KtExpression,
|
||||
val nullableExpression: KtExpression
|
||||
) : KotlinQuickFixAction<KtExpression>(expression) {
|
||||
|
||||
override fun getFamilyName() = text
|
||||
|
||||
override fun getText() = "Wrap with '?.let { ... }' call"
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val factory = KtPsiFactory(element)
|
||||
val nullableText = nullableExpression.text
|
||||
nullableExpression.replace(factory.createExpression("it"))
|
||||
val wrapped = factory.createExpressionByPattern("$0?.let { $1 }", nullableText, element.text)
|
||||
element.replace(wrapped)
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val element = diagnostic.psiElement
|
||||
val expression = element.getParentOfType<KtExpression>(true) ?: return null
|
||||
|
||||
val parent = element.parent
|
||||
val nullableExpression = (parent as? KtCallExpression)?.calleeExpression ?: return null
|
||||
|
||||
return WrapWithSafeLetCallFix(expression, nullableExpression)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Wrap with '?.let { ... }' call" "false"
|
||||
// WITH_RUNTIME
|
||||
// ACTION: Add non-null asserted (!!) call
|
||||
// ACTION: Introduce local variable
|
||||
// ACTION: Replace with safe (?.) call
|
||||
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
|
||||
|
||||
fun foo(arg: Int?) {
|
||||
arg?.hashCode()<caret>.toLong()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Wrap with '?.let { ... }' call" "false"
|
||||
// WITH_RUNTIME
|
||||
// ACTION: Add non-null asserted (!!) call
|
||||
// ACTION: Convert to block body
|
||||
// ACTION: Introduce local variable
|
||||
// ACTION: Replace with safe (?.) call
|
||||
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
|
||||
|
||||
fun foo(arg: Int?) = arg<caret>.hashCode()
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Wrap with '?.let { ... }' call" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(exec: (() -> Unit)?) {
|
||||
<caret>exec()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Wrap with '?.let { ... }' call" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun foo(exec: (() -> Unit)?) {
|
||||
exec?.let { it() }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Wrap with '?.let { ... }' call" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
operator fun Int.invoke() = this
|
||||
|
||||
fun foo(arg: Int?) {
|
||||
<caret>arg()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Wrap with '?.let { ... }' call" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
operator fun Int.invoke() = this
|
||||
|
||||
fun foo(arg: Int?) {
|
||||
arg?.let { it() }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Wrap with '?.let { ... }' call" "false"
|
||||
// WITH_RUNTIME
|
||||
// ACTION: Add non-null asserted (!!) call
|
||||
// ACTION: Introduce local variable
|
||||
// ACTION: Replace with safe (?.) call
|
||||
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?
|
||||
|
||||
class My(var x: Int?) {
|
||||
|
||||
fun foo() {
|
||||
x<caret>.hashCode()
|
||||
}
|
||||
}
|
||||
@@ -8761,4 +8761,43 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/wrapWithSafeLetCall")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class WrapWithSafeLetCall extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInWrapWithSafeLetCall() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/wrapWithSafeLetCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("chainedUnsafeCall.kt")
|
||||
public void testChainedUnsafeCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/chainedUnsafeCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("expressionUnsafeCall.kt")
|
||||
public void testExpressionUnsafeCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/expressionUnsafeCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("invokeFuncUnsafe.kt")
|
||||
public void testInvokeFuncUnsafe() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/invokeFuncUnsafe.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("invokeUnsafe.kt")
|
||||
public void testInvokeUnsafe() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/invokeUnsafe.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unstableValue.kt")
|
||||
public void testUnstableValue() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/unstableValue.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user