Merge ReplaceJavaIntegerToStringWithMemberInspection and ReplaceJavaStaticMethodWithTopLevelFunctionInspection

Relates to #KT-12721
This commit is contained in:
Dmitry Gridin
2019-04-11 11:27:14 +07:00
parent 6b7f4dda31
commit 6afc0367c0
19 changed files with 72 additions and 116 deletions
@@ -3360,15 +3360,6 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceJavaIntegerToStringWithMemberInspection"
displayName="Replace 'Integer.toString(int)' with 'int.toString()'"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceNotNullAssertionWithElvisReturnInspection"
displayName="Replace '!!' with '?: return'"
groupPath="Kotlin"
@@ -1,5 +0,0 @@
<html>
<body>
This inspection reports explicit calls of <b>Integer.toString(int)</b> which can be replaced with <b>int.toString()</b>
</body>
</html>
@@ -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"
}
@@ -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 }
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.inspections.ReplaceJavaIntegerToStringWithMemberInspection
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = Integer.<caret>toString(5) + 7
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 5.toString() + 7
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun foo() {
val b = listOf(42, 10)
println(Integer<caret>.toString(b.first(), b.last()).let{it} + 1)
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun foo() {
val b = listOf(42, 10)
println(b.first().toString(b.last()).let{it} + 1)
}
@@ -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")