From 66bd9d63dd76292ff85c76646a48a3d1edeff7d5 Mon Sep 17 00:00:00 2001 From: Dmitry Neverov Date: Thu, 22 Jun 2017 07:39:59 +0300 Subject: [PATCH] Do not suggest adding !! on null constant #KT-14643 Fixed --- .../jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt | 6 +++++- idea/testData/quickfix/addExclExclCall/null.kt | 8 ++++++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 idea/testData/quickfix/addExclExclCall/null.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt index 18bbf9b64c7..04d843b1e3b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic 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.isNullExpression import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* @@ -106,7 +107,10 @@ class AddExclExclCallFix(psiElement: PsiElement, val checkImplicitReceivers: Boo private fun getExpressionForIntroduceCall(): ExpressionForCall? { val psiElement = element ?: return null - return if (psiElement is LeafPsiElement && psiElement.elementType == KtTokens.DOT) { + return if ((psiElement as? KtExpression).isNullExpression()) { + return null + } + else if (psiElement is LeafPsiElement && psiElement.elementType == KtTokens.DOT) { (psiElement.prevSibling as? KtExpression).expressionForCall() } else if (psiElement is KtArrayAccessExpression) { diff --git a/idea/testData/quickfix/addExclExclCall/null.kt b/idea/testData/quickfix/addExclExclCall/null.kt new file mode 100644 index 00000000000..693783e8e27 --- /dev/null +++ b/idea/testData/quickfix/addExclExclCall/null.kt @@ -0,0 +1,8 @@ +// "Add non-null asserted (!!) call" "false" +// ACTION: Add 'const' modifier +// ACTION: Add 'toString()' call +// ACTION: Change type of 'x' to 'String?' +// ACTION: Convert property initializer to getter +// ERROR: Null can not be a value of a non-null type String + +val x: String = null \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index a256f89035b..da2eb4612a5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -266,6 +266,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("null.kt") + public void testNull() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addExclExclCall/null.kt"); + doTest(fileName); + } + @TestMetadata("operationIn.kt") public void testOperationIn() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addExclExclCall/operationIn.kt");