From 8969e7a6e1df1806dc0bc6586d4c386a21e0bad4 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 25 May 2018 05:27:46 +0300 Subject: [PATCH] "Remove redundant toString": fix false positive for nested nullable So #KT-24557 Fixed --- ...RemoveRedundantCallsOfConversionMethodsIntention.kt | 8 +++++--- .../nullable2.kt | 7 +++++++ .../safeString2.kt | 6 ++++++ .../safeString2.kt.after | 6 ++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 10 ++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable2.kt create mode 100644 idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt create mode 100644 idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt index 654a64df534..e97293c6397 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveRedundantCallsOfConversionMethodsIntention.kt @@ -82,9 +82,11 @@ class RemoveRedundantCallsOfConversionMethodsIntention : SelfTargetingRangeInten Boolean::class.qualifiedName } else { resolvedCall?.candidateDescriptor?.returnType?.let { - if (it.isFlexible()) null - else if (it.isMarkedNullable && parent !is KtSafeQualifiedExpression) null - else it.getFqNameAsString() + when { + it.isFlexible() -> null + parent !is KtSafeQualifiedExpression && (this is KtSafeQualifiedExpression || it.isMarkedNullable) -> null + else -> it.getFqNameAsString() + } } } } diff --git a/idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable2.kt b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable2.kt new file mode 100644 index 00000000000..b6f2abdb6a9 --- /dev/null +++ b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable2.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +data class Foo(val name: String) + +fun nullable2(foo: Foo?) { + val s: String = foo?.name.toString() +} \ No newline at end of file diff --git a/idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt new file mode 100644 index 00000000000..27a54fc9df0 --- /dev/null +++ b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME +data class Foo(val name: String) + +fun test(foo: Foo?) { + val s: String? = foo?.name?.toString() +} \ No newline at end of file diff --git a/idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt.after b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt.after new file mode 100644 index 00000000000..9c07929e4ca --- /dev/null +++ b/idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME +data class Foo(val name: String) + +fun test(foo: Foo?) { + val s: String? = foo?.name +} \ 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 0f8b7e91db6..e4675305e64 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -13242,11 +13242,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable.kt"); } + @TestMetadata("nullable2.kt") + public void testNullable2() throws Exception { + runTest("idea/testData/intentions/removeRedundantCallsOfConversionMethods/nullable2.kt"); + } + @TestMetadata("safeString.kt") public void testSafeString() throws Exception { runTest("idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString.kt"); } + @TestMetadata("safeString2.kt") + public void testSafeString2() throws Exception { + runTest("idea/testData/intentions/removeRedundantCallsOfConversionMethods/safeString2.kt"); + } + @TestMetadata("short.kt") public void testShort() throws Exception { runTest("idea/testData/intentions/removeRedundantCallsOfConversionMethods/short.kt");