Surround with null check: suggested for an assignment
#KT-30215 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
2314a38342
commit
1b7627e039
@@ -104,17 +104,22 @@ class SurroundWithNullCheckFix(
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val typeMismatch = Errors.TYPE_MISMATCH.cast(diagnostic)
|
||||
val nullableExpression = typeMismatch.psiElement as? KtReferenceExpression ?: return null
|
||||
val argument = nullableExpression.parent as? KtValueArgument ?: return null
|
||||
val call = argument.getParentOfType<KtCallExpression>(true) ?: return null
|
||||
|
||||
val rootCall = call.getLastParentOfTypeInRow<KtQualifiedExpression>() ?: call
|
||||
if (rootCall.parent !is KtBlockExpression) return null
|
||||
|
||||
val parent = nullableExpression.parent
|
||||
val root = when (parent) {
|
||||
is KtValueArgument -> {
|
||||
val call = parent.getParentOfType<KtCallExpression>(true) ?: return null
|
||||
call.getLastParentOfTypeInRow<KtQualifiedExpression>() ?: call
|
||||
}
|
||||
is KtBinaryExpression -> {
|
||||
if (parent.right != nullableExpression) return null
|
||||
parent
|
||||
}
|
||||
else -> return null
|
||||
}
|
||||
if (root.parent !is KtBlockExpression) return null
|
||||
if (!isNullabilityMismatch(expected = typeMismatch.a, actual = typeMismatch.b)) return null
|
||||
|
||||
if (!nullableExpression.isStableSimpleExpression()) return null
|
||||
|
||||
return SurroundWithNullCheckFix(rootCall, nullableExpression)
|
||||
return SurroundWithNullCheckFix(root, nullableExpression)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Surround with null check" "true"
|
||||
|
||||
fun foo(s: String?) {
|
||||
var ss: String = ""
|
||||
ss = <caret>s
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Surround with null check" "true"
|
||||
|
||||
fun foo(s: String?) {
|
||||
var ss: String = ""
|
||||
if (s != null) {
|
||||
ss = s
|
||||
}
|
||||
}
|
||||
@@ -11526,6 +11526,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
runTest("idea/testData/quickfix/surroundWithNullCheck/argumentNullable.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assignment.kt")
|
||||
public void testAssignment() throws Exception {
|
||||
runTest("idea/testData/quickfix/surroundWithNullCheck/assignment.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("chainedUnsafeCall.kt")
|
||||
public void testChainedUnsafeCall() throws Exception {
|
||||
runTest("idea/testData/quickfix/surroundWithNullCheck/chainedUnsafeCall.kt");
|
||||
|
||||
Reference in New Issue
Block a user