From 6f4ae2585b67fb37f1a763b0958b094a77e3b8fa Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 22 Mar 2017 18:00:47 +0300 Subject: [PATCH] Fix "surround with null check": place check correctly for unsafe call So #KT-16928 Fixed --- .../kotlin/idea/quickfix/SurroundWithNullCheckFix.kt | 7 ++++++- .../surroundWithNullCheck/objectNestedQualifiers.kt | 10 ++++++++++ .../objectNestedQualifiers.kt.after | 12 ++++++++++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 6 ++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt create mode 100644 idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/SurroundWithNullCheckFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/SurroundWithNullCheckFix.kt index 2ab0fa6e820..4605f0d9e49 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/SurroundWithNullCheckFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/SurroundWithNullCheckFix.kt @@ -57,6 +57,11 @@ class SurroundWithNullCheckFix( companion object : KotlinSingleIntentionActionFactory() { + private fun KtExpression.hasAcceptableParent() = with (parent) { + this is KtBlockExpression || this.parent is KtIfExpression || + this is KtWhenEntry || this.parent is KtLoopExpression + } + override fun createAction(diagnostic: Diagnostic): IntentionAction? { val element = diagnostic.psiElement val expressionParent = element.getParentOfType(strict = element is KtOperationReferenceExpression) ?: return null @@ -74,7 +79,7 @@ class SurroundWithNullCheckFix( if (!nullableExpression.isStable(context)) return null val expressionTarget = expressionParent.getParentOfTypesAndPredicate(strict = false, parentClasses = KtExpression::class.java) { - !it.isUsedAsExpression(context) + !it.isUsedAsExpression(context) && it.hasAcceptableParent() } ?: return null // Surround declaration (even of local variable) with null check is generally a bad idea if (expressionTarget is KtDeclaration) return null diff --git a/idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt b/idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt new file mode 100644 index 00000000000..b0a22833a1b --- /dev/null +++ b/idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt @@ -0,0 +1,10 @@ +// "Surround with null check" "true" + +fun foo(p: String?) { + Util.f1(Util.f2(p.length), 0) +} + +object Util { + fun f1(o: Any, p: Int): Any = o + fun f2(o: Any): Any = o +} \ No newline at end of file diff --git a/idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt.after b/idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt.after new file mode 100644 index 00000000000..06555aa3996 --- /dev/null +++ b/idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt.after @@ -0,0 +1,12 @@ +// "Surround with null check" "true" + +fun foo(p: String?) { + if (p != null) { + Util.f1(Util.f2(p.length), 0) + } +} + +object Util { + fun f1(o: Any, p: Int): Any = o + fun f2(o: Any): Any = o +} \ 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 af220b1f545..231b2b8f976 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -9126,6 +9126,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("objectNestedQualifiers.kt") + public void testObjectNestedQualifiers() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt"); + doTest(fileName); + } + @TestMetadata("objectQualifier.kt") public void testObjectQualifier() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/objectQualifier.kt");