mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[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
This commit is contained in:
committed by
intellij-monorepo-bot
parent
20b29e7775
commit
6a88044045
+9
-3
@@ -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<KtConstantExpression>()?.text?.toIntOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -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)
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_STDLIB
|
||||
fun test(charArray: CharArray, len: Int): String {
|
||||
return buildString {
|
||||
<caret>append(charArray, 0, len)
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_STDLIB
|
||||
fun test(charArray: CharArray, len: Int): String {
|
||||
return buildString {
|
||||
appendRange(charArray, 0, len)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user