From 44ca45123dd0ac06034d1f16db19c850ffe856b5 Mon Sep 17 00:00:00 2001 From: shiraji Date: Fri, 9 Dec 2016 00:25:55 +0900 Subject: [PATCH] KT-15142 Remove redundant calls of the conversion method wrongly shown for Any?.toString #KT-15142 Fixed --- .../RemoveRedundantCallsOfConversionMethodsIntention.kt | 6 ++---- .../removeRedundantCallsOfConversionMethods/nullable.kt | 5 +++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt index 6d7aae08a57..3b893eca7e6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt @@ -23,10 +23,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName -import org.jetbrains.kotlin.psi.KtConstantExpression -import org.jetbrains.kotlin.psi.KtExpression -import org.jetbrains.kotlin.psi.KtQualifiedExpression -import org.jetbrains.kotlin.psi.KtStringTemplateExpression +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.types.isFlexible @@ -69,6 +66,7 @@ class RemoveRedundantCallsOfConversionMethodsIntention : SelfTargetingRangeInten else -> { getResolvedCall(analyze())?.candidateDescriptor?.returnType?.let { if (it.isFlexible()) null + else if (it.isMarkedNullable && parent !is KtSafeQualifiedExpression) null else it.getJetTypeFqName(false) } } diff --git a/idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable.kt b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable.kt new file mode 100644 index 00000000000..4ed689402f7 --- /dev/null +++ b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME +// IS_APPLICABLE: false +fun foo(s: String?) { + val t: String = s.toString() +} \ 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 00e8419a34c..1deba1e1b61 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -11805,6 +11805,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("nullable.kt") + public void testNullable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable.kt"); + doTest(fileName); + } + @TestMetadata("safeString.kt") public void testSafeString() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString.kt");