KT-11104 extra : "wrap with safe let call" is now applicable for argument type mismatches

This commit is contained in:
Mikhail Glukhikh
2016-06-06 16:43:59 +03:00
committed by Mikhail Glukhikh
parent 47c1106d5d
commit 3c49b5f342
5 changed files with 46 additions and 4 deletions
@@ -194,9 +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(WrapWithSafeLetCallFix.UnsafeFactory)
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
UNSAFE_INFIX_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
TYPE_MISMATCH.registerFactory(WrapWithSafeLetCallFix.TypeMismatchFactory)
UNSAFE_CALL.registerFactory(AddExclExclCallFix)
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.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
class WrapWithSafeLetCallFix(
expression: KtExpression,
@@ -40,7 +43,7 @@ class WrapWithSafeLetCallFix(
element.replace(wrapped)
}
companion object : KotlinSingleIntentionActionFactory() {
object UnsafeFactory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = diagnostic.psiElement
val expression = element.getParentOfType<KtExpression>(true) ?: return null
@@ -51,4 +54,20 @@ class WrapWithSafeLetCallFix(
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)
}
}
}