From 09ae1f1a5d1a990c11224c3d75ed1a5e9004528b Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 14 Nov 2016 13:21:34 +0300 Subject: [PATCH] False positive removed in replace single line let intention #KT-14791 Fixed --- .../ReplaceSingleLineLetIntention.kt | 3 +- .../assignment.kt | 8 +++++ .../comparisons.kt | 4 +++ .../plusNullable.kt | 4 +++ .../smartCastInBody.kt | 15 ++++++++++ .../typeChecks.kt | 9 ++++++ .../intentions/IntentionTestGenerated.java | 30 +++++++++++++++++++ 7 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/assignment.kt create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/comparisons.kt create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/plusNullable.kt create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/smartCastInBody.kt create mode 100644 idea/testData/intentions/replaceSingleLineLetIntention/typeChecks.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt index f214890da55..7cad0320a47 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt @@ -94,7 +94,7 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention bodyExpression.isApplicable(parameterName) + is KtBinaryExpression -> element.parent !is KtSafeQualifiedExpression && bodyExpression.isApplicable(parameterName) is KtDotQualifiedExpression -> bodyExpression.isApplicable(parameterName) else -> false } @@ -105,6 +105,7 @@ class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention if (left.text != parameterName) return false is KtDotQualifiedExpression -> if (!left.isApplicable(parameterName)) return false + else -> return false } val right = right ?: return false diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/assignment.kt b/idea/testData/intentions/replaceSingleLineLetIntention/assignment.kt new file mode 100644 index 00000000000..99e219582c8 --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/assignment.kt @@ -0,0 +1,8 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME + +fun withAssign(arg: String?): String { + var result: String = "" + arg?.let { result = it } + return result +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/comparisons.kt b/idea/testData/intentions/replaceSingleLineLetIntention/comparisons.kt new file mode 100644 index 00000000000..85b18e7f551 --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/comparisons.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME + +fun isAlphaOrBeta(str: String) = str.let { it == "Alpha" || it == "Beta" } diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/plusNullable.kt b/idea/testData/intentions/replaceSingleLineLetIntention/plusNullable.kt new file mode 100644 index 00000000000..f24ae2bf4af --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/plusNullable.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME + +fun plusNullable(arg: String?) = arg?.let { it + "#" } ?: "" \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/smartCastInBody.kt b/idea/testData/intentions/replaceSingleLineLetIntention/smartCastInBody.kt new file mode 100644 index 00000000000..81862c70028 --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/smartCastInBody.kt @@ -0,0 +1,15 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME + +interface A + +interface B : A { + val bar: Any? +} + +class Foo { + private val a: A = object : A {} + + val isB: Boolean + get() = a.let { it is B && it.bar != null } +} \ No newline at end of file diff --git a/idea/testData/intentions/replaceSingleLineLetIntention/typeChecks.kt b/idea/testData/intentions/replaceSingleLineLetIntention/typeChecks.kt new file mode 100644 index 00000000000..5f629178f55 --- /dev/null +++ b/idea/testData/intentions/replaceSingleLineLetIntention/typeChecks.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME + +interface Base + +class A : Base +class B : Base + +fun isAB(arg: Base) = arg.let { it is A || it is B } \ 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 f1af0a0dfbc..ca17ef95796 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -11799,6 +11799,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSingleLineLetIntention"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("assignment.kt") + public void testAssignment() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/assignment.kt"); + doTest(fileName); + } + + @TestMetadata("comparisons.kt") + public void testComparisons() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/comparisons.kt"); + doTest(fileName); + } + @TestMetadata("lambdaWithBinaryExpression.kt") public void testLambdaWithBinaryExpression() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/lambdaWithBinaryExpression.kt"); @@ -11949,6 +11961,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("plusNullable.kt") + public void testPlusNullable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/plusNullable.kt"); + doTest(fileName); + } + @TestMetadata("receiverWithLambda.kt") public void testReceiverWithLambda() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/receiverWithLambda.kt"); @@ -11967,6 +11985,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("smartCastInBody.kt") + public void testSmartCastInBody() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/smartCastInBody.kt"); + doTest(fileName); + } + @TestMetadata("this.kt") public void testThis() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/this.kt"); @@ -11978,6 +12002,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/thisShort.kt"); doTest(fileName); } + + @TestMetadata("typeChecks.kt") + public void testTypeChecks() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSingleLineLetIntention/typeChecks.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/intentions/replaceSizeCheckWithIsNotEmpty")