[kotlin] "Java methods should be replaced with Kotlin analog": fix toString with radix

^KTIJ-25660 Fixed

GitOrigin-RevId: 71ef244b3e5fd69b4b270c67df23a1eccd09388b
This commit is contained in:
Alexey Belkov
2023-05-29 12:38:34 +00:00
committed by intellij-monorepo-bot
parent 73d660ea31
commit 352fbceb05
9 changed files with 46 additions and 3 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKot
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.callExpressionVisitor
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
@@ -87,8 +88,19 @@ class ReplaceJavaStaticMethodWithKotlinAnalogInspection : AbstractKotlinInspecti
"Float" to "Float"
).flatMap { (javaPrimitive, kotlinPrimitive) ->
listOf(
// If radix is not a literal, it is considered an unsafe replacement (Kotlin has checks for an invalid radix)
Replacement("java.lang.$javaPrimitive.toString", "kotlin.text.toString", ToExtensionFunctionWithNonNullableReceiver, mayChangeSemantics = true) {
if (it.valueArguments.size != 2) return@Replacement false
val radix = it.valueArguments.last().getArgumentExpression()
radix !is KtConstantExpression
},
// If radix is an int literal within bounds, the conversion is safe
Replacement("java.lang.$javaPrimitive.toString", "kotlin.text.toString", ToExtensionFunctionWithNonNullableReceiver) {
it.valueArguments.size == 2
if (it.valueArguments.size != 2) return@Replacement false
val radixExpr = it.valueArguments.last().getArgumentExpression()
if (radixExpr !is KtConstantExpression) return@Replacement false
val radix = radixExpr.text.toIntOrNull() ?: return@Replacement false
radix in 2..36
},
Replacement(
"java.lang.$javaPrimitive.toString",
@@ -12739,6 +12739,16 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/intToString2.kt");
}
@TestMetadata("intToStringWithInvalidLiteralRadix.kt")
public void testIntToStringWithInvalidLiteralRadix() throws Exception {
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/intToStringWithInvalidLiteralRadix.kt");
}
@TestMetadata("intToStringWithLiteralRadix.kt")
public void testIntToStringWithLiteralRadix() throws Exception {
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/intToStringWithLiteralRadix.kt");
}
@TestMetadata("longToString.kt")
public void testLongToString() throws Exception {
runTest("testData/inspectionsLocal/replaceJavaStaticMethodWithKotlinAnalog/toString/longToString.kt");
@@ -1,4 +1,5 @@
// WITH_STDLIB
// FIX: Replace with 'toString' function (may change semantics)
fun foo() {
val b = listOf(42, 10)
@@ -1,4 +1,5 @@
// WITH_STDLIB
// FIX: Replace with 'toString' function (may change semantics)
fun foo() {
val b = listOf(42, 10)
@@ -0,0 +1,5 @@
// WITH_STDLIB
// PROBLEM: none
fun foo() {
Integer.<caret>toString(42, 1)
}
@@ -0,0 +1,5 @@
// WITH_STDLIB
fun foo() {
Integer.<caret>toString(42, 8)
}
@@ -1,5 +1,7 @@
// WITH_STDLIB
// FIX: Replace with 'toString' function (may change semantics)
fun foo() {
val t = java.lang.Long.<caret>toString(5, 42) + 6
val radix = 42
val t = java.lang.Long.<caret>toString(5, radix) + 6
}
@@ -1,5 +1,7 @@
// WITH_STDLIB
// FIX: Replace with 'toString' function (may change semantics)
fun foo() {
val t = 5.toString(42) + 6
val radix = 42
val t = 5.toString(radix) + 6
}