From 0fd42bc7470c03b4039c86a5909c88814ef556ae Mon Sep 17 00:00:00 2001 From: Dmitry Neverov Date: Wed, 24 May 2017 07:42:06 +0300 Subject: [PATCH] Don't suggest cast when !! is possible #KT-18033 Fixed --- .../kotlin/idea/quickfix/CastExpressionFix.kt | 2 ++ .../quickfix/typeMismatch/casts/preferExclExcl.kt | 15 +++++++++++++++ .../idea/quickfix/QuickFixTestGenerated.java | 6 ++++++ 3 files changed, 23 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/casts/preferExclExcl.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt index cb6dd46c569..3c396b5d4fa 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/CastExpressionFix.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.asFlexibleType import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf +import org.jetbrains.kotlin.types.typeUtil.makeNullable class CastExpressionFix(element: KtExpression, type: KotlinType) : KotlinQuickFixAction(element) { private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type) @@ -39,6 +40,7 @@ class CastExpressionFix(element: KtExpression, type: KotlinType) : KotlinQuickFi private val upOrDownCast: Boolean = run { val expressionType = element.analyze(BodyResolveMode.PARTIAL).getType(element) expressionType != null && (type.isSubtypeOf(expressionType) || expressionType.isSubtypeOf(type)) + && expressionType != type.makeNullable() //covered by AddExclExclCallFix } override fun getFamilyName() = "Cast expression" diff --git a/idea/testData/quickfix/typeMismatch/casts/preferExclExcl.kt b/idea/testData/quickfix/typeMismatch/casts/preferExclExcl.kt new file mode 100644 index 00000000000..58b40f6697c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/casts/preferExclExcl.kt @@ -0,0 +1,15 @@ +// "Cast expression 's' to 'String'" "false" +// ACTION: Add 'toString()' call +// ACTION: Add non-null asserted (!!) call +// ACTION: Change parameter 's' type of function 'bar' to 'String?' +// ACTION: Convert to expression body +// ACTION: Create function 'bar' +// ACTION: Surround with null check +// ACTION: Wrap with '?.let { ... }' call +// ERROR: Type mismatch: inferred type is String? but String was expected + +fun foo(s: String?) { + bar(s) +} + +fun bar(s: String){} \ 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 43365666f4a..c0292ae9a7c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -10291,6 +10291,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("preferExclExcl.kt") + public void testPreferExclExcl() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/casts/preferExclExcl.kt"); + doTest(fileName); + } + @TestMetadata("smartcastImpossible1.kt") public void testSmartcastImpossible1() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/casts/smartcastImpossible1.kt");