KT-11104 extra : "wrap with safe let call" is now applicable for argument type mismatches
This commit is contained in:
committed by
Mikhail Glukhikh
parent
47c1106d5d
commit
3c49b5f342
@@ -194,9 +194,10 @@ class QuickFixRegistrar : QuickFixContributor {
|
|||||||
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(SurroundWithNullCheckFix)
|
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(SurroundWithNullCheckFix)
|
||||||
UNSAFE_INFIX_CALL.registerFactory(SurroundWithNullCheckFix)
|
UNSAFE_INFIX_CALL.registerFactory(SurroundWithNullCheckFix)
|
||||||
|
|
||||||
UNSAFE_CALL.registerFactory(WrapWithSafeLetCallFix)
|
UNSAFE_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
|
||||||
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(WrapWithSafeLetCallFix)
|
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
|
||||||
UNSAFE_INFIX_CALL.registerFactory(WrapWithSafeLetCallFix)
|
UNSAFE_INFIX_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
|
||||||
|
TYPE_MISMATCH.registerFactory(WrapWithSafeLetCallFix.TypeMismatchFactory)
|
||||||
|
|
||||||
UNSAFE_CALL.registerFactory(AddExclExclCallFix)
|
UNSAFE_CALL.registerFactory(AddExclExclCallFix)
|
||||||
UNNECESSARY_NOT_NULL_ASSERTION.registerFactory(RemoveExclExclCallFix)
|
UNNECESSARY_NOT_NULL_ASSERTION.registerFactory(RemoveExclExclCallFix)
|
||||||
|
|||||||
@@ -20,8 +20,11 @@ import com.intellij.codeInsight.intention.IntentionAction
|
|||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Errors
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
|
import org.jetbrains.kotlin.types.TypeUtils
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||||
|
|
||||||
class WrapWithSafeLetCallFix(
|
class WrapWithSafeLetCallFix(
|
||||||
expression: KtExpression,
|
expression: KtExpression,
|
||||||
@@ -40,7 +43,7 @@ class WrapWithSafeLetCallFix(
|
|||||||
element.replace(wrapped)
|
element.replace(wrapped)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
object UnsafeFactory : KotlinSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
val element = diagnostic.psiElement
|
val element = diagnostic.psiElement
|
||||||
val expression = element.getParentOfType<KtExpression>(true) ?: return null
|
val expression = element.getParentOfType<KtExpression>(true) ?: return null
|
||||||
@@ -51,4 +54,20 @@ class WrapWithSafeLetCallFix(
|
|||||||
return WrapWithSafeLetCallFix(expression, nullableExpression)
|
return WrapWithSafeLetCallFix(expression, nullableExpression)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object TypeMismatchFactory : KotlinSingleIntentionActionFactory() {
|
||||||
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
|
val typeMismatch = Errors.TYPE_MISMATCH.cast(diagnostic)
|
||||||
|
val argument = typeMismatch.psiElement.parent as? KtValueArgument ?: return null
|
||||||
|
val call = argument.getParentOfType<KtCallExpression>(true) ?: return null
|
||||||
|
|
||||||
|
val expected = typeMismatch.a
|
||||||
|
val actual = typeMismatch.b
|
||||||
|
if (expected.isMarkedNullable || !actual.isMarkedNullable) return null
|
||||||
|
val expectedNullable = TypeUtils.makeNullable(expected)
|
||||||
|
if (!actual.isSubtypeOf(expectedNullable)) return null
|
||||||
|
|
||||||
|
return WrapWithSafeLetCallFix(call, typeMismatch.psiElement)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Wrap with '?.let { ... }' call" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(x: String?) {
|
||||||
|
bar(<caret>x)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(s: String) = s.hashCode()
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// "Wrap with '?.let { ... }' call" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(x: String?) {
|
||||||
|
x?.let { bar(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(s: String) = s.hashCode()
|
||||||
@@ -8770,6 +8770,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/wrapWithSafeLetCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/wrapWithSafeLetCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("argumentNullable.kt")
|
||||||
|
public void testArgumentNullable() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/argumentNullable.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("chainedUnsafeCall.kt")
|
@TestMetadata("chainedUnsafeCall.kt")
|
||||||
public void testChainedUnsafeCall() throws Exception {
|
public void testChainedUnsafeCall() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/chainedUnsafeCall.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/wrapWithSafeLetCall/chainedUnsafeCall.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user