diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml index d7acb3e904e..3127790dbf1 100644 --- a/idea/resources/META-INF/plugin-common.xml +++ b/idea/resources/META-INF/plugin-common.xml @@ -3360,15 +3360,6 @@ language="kotlin" /> - - - -This inspection reports explicit calls of Integer.toString(int) which can be replaced with int.toString() - - diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaIntegerToStringWithMemberInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaIntegerToStringWithMemberInspection.kt deleted file mode 100644 index b4b97851bcf..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaIntegerToStringWithMemberInspection.kt +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.idea.inspections - -import com.intellij.codeInspection.LocalQuickFix -import com.intellij.codeInspection.ProblemDescriptor -import com.intellij.codeInspection.ProblemsHolder -import com.intellij.openapi.project.Project -import com.intellij.psi.PsiElementVisitor -import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall -import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe - -class ReplaceJavaIntegerToStringWithMemberInspection : AbstractKotlinInspection() { - override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { - return dotQualifiedExpressionVisitor { dotQualifiedExpression -> - if (dotQualifiedExpression.isToString()) { - holder.registerProblem( - dotQualifiedExpression, - "Could be replaced with `int.toString()`", - ConvertIntegerToStringQuickFix() - ) - } - } - } -} - -private class ConvertIntegerToStringQuickFix : LocalQuickFix { - override fun getName() = "Replace with 'int.toString()'" - - override fun getFamilyName() = name - - override fun applyFix(project: Project, descriptor: ProblemDescriptor) { - val dotQualifiedExpression = descriptor.psiElement as? KtDotQualifiedExpression ?: return - val element = dotQualifiedExpression.selectorExpression as? KtCallExpression ?: return - val arguments = element.valueArguments - val integerArg = arguments.getOrNull(0) ?: return - val radixArg = arguments.getOrNull(1) - - dotQualifiedExpression.replace( - KtPsiFactory(element).createExpressionByPattern( - "$0.toString($1)", - integerArg.getArgumentExpression() ?: return, - radixArg?.getArgumentExpression() ?: "" - ) - ) - } -} - -private fun KtDotQualifiedExpression.isToString(): Boolean { - val calleeExpression = (selectorExpression as? KtCallExpression)?.calleeExpression ?: return false - val nameReference = calleeExpression as? KtNameReferenceExpression ?: return false - if (nameReference.getReferencedName() != "toString") return false - - val resolvedCall = this.resolveToCall() ?: return false - if (!resolvedCall.status.isSuccess) return false - val fqName = resolvedCall.resultingDescriptor.fqNameUnsafe.asString() - return fqName == "java.lang.Integer.toString" -} diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt index 809e7eceb5e..71dca7f616c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceJavaStaticMethodWithTopLevelFunctionInspection.kt @@ -153,7 +153,8 @@ class ReplaceJavaStaticMethodWithTopLevelFunctionInspection : AbstractKotlinInsp Replacement("java.lang.Math.tan", "kotlin.math.tan"), Replacement("java.lang.Math.tanh", "kotlin.math.tanh"), Replacement("java.lang.Math.copySign", "kotlin.math.withSign", toExtensionFunction = true), - Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true) + Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true), + Replacement("java.lang.Integer.toString", "kotlin.text.toString", toExtensionFunction = true) ).groupBy { it.javaMethodShortName } } } diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/.inspection b/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/.inspection deleted file mode 100644 index 9d06d60edc8..00000000000 --- a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/.inspection +++ /dev/null @@ -1 +0,0 @@ -org.jetbrains.kotlin.idea.inspections.ReplaceJavaIntegerToStringWithMemberInspection diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/incorrectDescriptor.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/incorrectDescriptor.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/incorrectDescriptor.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/incorrectDescriptor.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt new file mode 100644 index 00000000000..9e2d5550e4c --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + val t = Integer.toString(5) + 7 +} diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt.after new file mode 100644 index 00000000000..4e5484dc52c --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + val t = 5.toString() + 7 +} diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt new file mode 100644 index 00000000000..6dbb93fcb52 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME + +fun foo() { + val b = listOf(42, 10) + println(Integer.toString(b.first(), b.last()).let{it} + 1) +} diff --git a/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt.after new file mode 100644 index 00000000000..d5210db0dd3 --- /dev/null +++ b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt.after @@ -0,0 +1,6 @@ +// WITH_RUNTIME + +fun foo() { + val b = listOf(42, 10) + println(b.first().toString(b.last()).let{it} + 1) +} diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceIntToString.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceIntToString.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceIntToString.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceIntToString.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceIntToString.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceIntToString.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceIntToString.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceIntToString.kt.after diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceListElementToString.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceListElementToString.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceListElementToString.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceListElementToString.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceListElementToString.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceListElementToString.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceListElementToString.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceListElementToString.kt.after diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringBase.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringBase.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringBase.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringBase.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringBase.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringBase.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringBase.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringBase.kt.after diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringWithRadix.kt b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringWithRadix.kt similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringWithRadix.kt rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringWithRadix.kt diff --git a/idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringWithRadix.kt.after b/idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringWithRadix.kt.after similarity index 100% rename from idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringWithRadix.kt.after rename to idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringWithRadix.kt.after diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 6df2e2a7a7a..90ae8b2d501 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -7138,44 +7138,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } - @TestMetadata("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class ReplaceJavaIntegerToStringWithMember extends AbstractLocalInspectionTest { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); - } - - public void testAllFilesPresentInReplaceJavaIntegerToStringWithMember() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); - } - - @TestMetadata("incorrectDescriptor.kt") - public void testIncorrectDescriptor() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/incorrectDescriptor.kt"); - } - - @TestMetadata("replaceIntToString.kt") - public void testReplaceIntToString() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceIntToString.kt"); - } - - @TestMetadata("replaceListElementToString.kt") - public void testReplaceListElementToString() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceListElementToString.kt"); - } - - @TestMetadata("replaceToStringBase.kt") - public void testReplaceToStringBase() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringBase.kt"); - } - - @TestMetadata("replaceToStringWithRadix.kt") - public void testReplaceToStringWithRadix() throws Exception { - runTest("idea/testData/inspectionsLocal/replaceJavaIntegerToStringWithMember/replaceToStringWithRadix.kt"); - } - } - @TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -7421,6 +7383,54 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/system/println_3.kt"); } } + + @TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ToString extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInToString() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("incorrectDescriptor.kt") + public void testIncorrectDescriptor() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/incorrectDescriptor.kt"); + } + + @TestMetadata("intToString.kt") + public void testIntToString() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString.kt"); + } + + @TestMetadata("intToString2.kt") + public void testIntToString2() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt"); + } + + @TestMetadata("replaceIntToString.kt") + public void testReplaceIntToString() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceIntToString.kt"); + } + + @TestMetadata("replaceListElementToString.kt") + public void testReplaceListElementToString() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceListElementToString.kt"); + } + + @TestMetadata("replaceToStringBase.kt") + public void testReplaceToStringBase() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringBase.kt"); + } + + @TestMetadata("replaceToStringWithRadix.kt") + public void testReplaceToStringWithRadix() throws Exception { + runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringWithRadix.kt"); + } + } } @TestMetadata("idea/testData/inspectionsLocal/replaceNegatedIsEmptyWithIsNotEmpty")