From c6db26ba91063b179e50cae72eb67f09301d16fe Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 9 Aug 2018 12:51:04 +0900 Subject: [PATCH] "Replace 'if' with elvis operator": Don't suggest on nullable type check #KT-25886 Fixed --- .../intentions/FoldInitializerAndIfToElvisIntention.kt | 1 + .../foldInitializerAndIfToElvis/NotIsNullableType.kt | 8 ++++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 5 +++++ 3 files changed, 14 insertions(+) create mode 100644 idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/FoldInitializerAndIfToElvisIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/FoldInitializerAndIfToElvisIntention.kt index a6086f7d117..8bec7c36609 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/FoldInitializerAndIfToElvisIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/FoldInitializerAndIfToElvisIntention.kt @@ -121,6 +121,7 @@ class FoldInitializerAndIfToElvisIntention : SelfTargetingRangeIntention { if (!operationExpression.isNegated) return null + if (operationExpression.typeReference?.typeElement is KtNullableType) return null operationExpression.leftHandSide } else -> return null diff --git a/idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt b/idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt new file mode 100644 index 00000000000..2a44b5136e8 --- /dev/null +++ b/idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false +fun test(): String { + val foo = foo() + if (foo !is String?) return "0" + return "1" +} + +fun foo(): Any? = null \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 174ec0f94d9..287ad63150e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -8436,6 +8436,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIs.kt"); } + @TestMetadata("NotIsNullableType.kt") + public void testNotIsNullableType() throws Exception { + runTest("idea/testData/intentions/foldInitializerAndIfToElvis/NotIsNullableType.kt"); + } + @TestMetadata("OtherVar1.kt") public void testOtherVar1() throws Exception { runTest("idea/testData/intentions/foldInitializerAndIfToElvis/OtherVar1.kt");