Wrap with safe let call : minor refactoring

This commit is contained in:
Mikhail Glukhikh
2016-06-13 15:42:04 +03:00
parent 954952ae71
commit 10935ba9db
2 changed files with 5 additions and 5 deletions
@@ -71,6 +71,9 @@ fun KotlinType?.isArrayOfNothing(): Boolean {
fun KotlinType.isSubtypeOf(superType: KotlinType): Boolean = KotlinTypeChecker.DEFAULT.isSubtypeOf(this, superType)
fun isNullabilityMismatch(expected: KotlinType, actual: KotlinType) =
!expected.isMarkedNullable && actual.isMarkedNullable && actual.isSubtypeOf(TypeUtils.makeNullable(expected))
fun KotlinType.cannotBeReified(): Boolean =
KotlinBuiltIns.isNothingOrNullableNothing(this) || this.isDynamic() || this.isCaptured()
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.isNullabilityMismatch
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
class WrapWithSafeLetCallFix(
@@ -68,11 +69,7 @@ class WrapWithSafeLetCallFix(
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
if (!isNullabilityMismatch(expected = typeMismatch.a, actual = typeMismatch.b)) return null
return WrapWithSafeLetCallFix(call, typeMismatch.psiElement)
}