From 6a880440457ee196b9519f90a4ce33b00f818e4c Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sun, 11 Sep 2022 20:19:48 +0900 Subject: [PATCH] [kotlin] "'StringBuilder.append(CharArray, offset, len)' call on the JVM": remove redundant zero ^KTIJ-22736 Fixed closes https://github.com/JetBrains/intellij-community/pull/2153 GitOrigin-RevId: c59aba21c9dd233236a4548eaa8424450b361f36 --- .../ReplaceWithStringBuilderAppendRangeInspection.kt | 12 +++++++++--- .../inspections/LocalInspectionTestGenerated.java | 5 +++++ .../zeroOffset.kt | 6 ++++++ .../zeroOffset.kt.after | 6 ++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt create mode 100644 plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt.after diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceWithStringBuilderAppendRangeInspection.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceWithStringBuilderAppendRangeInspection.kt index db464994aa94..a744e57cb147 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceWithStringBuilderAppendRangeInspection.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceWithStringBuilderAppendRangeInspection.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.platform.jvm.isJvm import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.types.typeUtil.isInt +import org.jetbrains.kotlin.utils.addToStdlib.safeAs class ReplaceWithStringBuilderAppendRangeInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool { companion object { @@ -65,14 +66,19 @@ class ReplaceWithStringBuilderAppendRangeInspection : AbstractKotlinInspection() val psiFactory = KtPsiFactory(callExpression) calleeExpression.replace(psiFactory.createCalleeExpression(functionName)) - if (secondArg is KtConstantExpression && thirdArg is KtConstantExpression) { - thirdArg.replace(psiFactory.createExpression(secondArg.text.toInt().plus(thirdArg.text.toInt()).toString())) - } else { + + val secondArgAsInt = secondArg.toIntOrNull() + val thirdArgAsInt = thirdArg.toIntOrNull() + if (secondArgAsInt != null && thirdArgAsInt != null) { + thirdArg.replace(psiFactory.createExpression(secondArgAsInt.plus(thirdArgAsInt).toString())) + } else if (secondArgAsInt != 0) { thirdArg.replace(psiFactory.createExpressionByPattern("$0 + $1", secondArg, thirdArg)) } } private fun KtPsiFactory.createCalleeExpression(functionName: String): KtExpression = (createExpression("$functionName()") as KtCallExpression).calleeExpression!! + + private fun KtExpression.toIntOrNull(): Int? = safeAs()?.text?.toIntOrNull() } } diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 132edd4e8ae8..5381131c3151 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -13303,6 +13303,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti public void testSimple() throws Exception { runTest("testData/inspectionsLocal/replaceWithStringBuilderAppendRange/simple.kt"); } + + @TestMetadata("zeroOffset.kt") + public void testZeroOffset() throws Exception { + runTest("testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt"); + } } @RunWith(JUnit3RunnerWithInners.class) diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt new file mode 100644 index 000000000000..e587e1eab063 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt @@ -0,0 +1,6 @@ +// WITH_STDLIB +fun test(charArray: CharArray, len: Int): String { + return buildString { + append(charArray, 0, len) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt.after b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt.after new file mode 100644 index 000000000000..8da3aa6f3db0 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/replaceWithStringBuilderAppendRange/zeroOffset.kt.after @@ -0,0 +1,6 @@ +// WITH_STDLIB +fun test(charArray: CharArray, len: Int): String { + return buildString { + appendRange(charArray, 0, len) + } +} \ No newline at end of file