Surround with null check: fix incorrect check for 'in' expression

#KT-31749 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-12-13 12:16:25 +09:00
committed by Ilya Kirillov
parent 1188f4617a
commit 11044a3ab5
4 changed files with 17 additions and 1 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableSimpleExpression
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import org.jetbrains.kotlin.psi.psiUtil.getLastParentOfTypeInRow
@@ -68,7 +69,7 @@ class SurroundWithNullCheckFix(
val nullableExpression =
when (parent) {
is KtDotQualifiedExpression -> parent.receiverExpression
is KtBinaryExpression -> parent.left
is KtBinaryExpression -> if (parent.operationToken == KtTokens.IN_KEYWORD) parent.right else parent.left
is KtCallExpression -> parent.calleeExpression
else -> return null
} as? KtReferenceExpression ?: return null
+4
View File
@@ -0,0 +1,4 @@
// "Surround with null check" "true"
fun test(a: String, b: List<String>?) {
a <caret>in b
}
@@ -0,0 +1,6 @@
// "Surround with null check" "true"
fun test(a: String, b: List<String>?) {
if (b != null) {
a in b
}
}
@@ -13161,6 +13161,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/surroundWithNullCheck/expressionUnsafeCall.kt");
}
@TestMetadata("in.kt")
public void testIn() throws Exception {
runTest("idea/testData/quickfix/surroundWithNullCheck/in.kt");
}
@TestMetadata("infixUnsafeCall.kt")
public void testInfixUnsafeCall() throws Exception {
runTest("idea/testData/quickfix/surroundWithNullCheck/infixUnsafeCall.kt");