Highlight use of toString() inside string interpolation as redundant #KT-10731 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
52dacd1d49
commit
d68a681db5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports calls to 'toString()' in String templates that can be safely removed
|
||||
</body>
|
||||
</html>
|
||||
@@ -1670,6 +1670,14 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RemoveToStringInStringTemplateInspection"
|
||||
displayName="Remove redundant call to 'toString()' in string template"
|
||||
groupName="Kotlin"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeInspection.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations
|
||||
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class RemoveToStringInStringTemplateInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
|
||||
object : KtVisitorVoid() {
|
||||
override fun visitCallExpression(expression: KtCallExpression) {
|
||||
val qualifiedExpression = expression.parent as? KtDotQualifiedExpression ?: return
|
||||
if (qualifiedExpression.parent !is KtBlockStringTemplateEntry) return
|
||||
if (!qualifiedExpression.isToString()) return
|
||||
|
||||
holder.registerProblem(expression,
|
||||
"Redundant 'toString()' call in string template",
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
||||
RemoveToStringFix())
|
||||
}
|
||||
|
||||
private fun KtDotQualifiedExpression.isToString(): Boolean {
|
||||
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
|
||||
val callableDescriptor = resolvedCall.resultingDescriptor as? CallableMemberDescriptor ?: return false
|
||||
return callableDescriptor.getDeepestSuperDeclarations().any { it.fqNameUnsafe.asString() == "kotlin.Any.toString" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RemoveToStringFix: LocalQuickFix {
|
||||
override fun getName() = "Remove 'toString()' call"
|
||||
override fun getFamilyName() = name
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val element = descriptor.psiElement.parent as? KtDotQualifiedExpression ?: return
|
||||
val receiverExpression = element.receiverExpression
|
||||
if (receiverExpression is KtNameReferenceExpression) {
|
||||
val templateEntry = receiverExpression.parent.parent
|
||||
if (templateEntry is KtBlockStringTemplateEntry && canPlaceAfterSimpleNameEntry(templateEntry.nextSibling)) {
|
||||
|
||||
val factory = KtPsiFactory(templateEntry)
|
||||
templateEntry.replace(factory.createSimpleNameStringTemplateEntry(receiverExpression.getReferencedName()))
|
||||
return
|
||||
}
|
||||
}
|
||||
element.replace(receiverExpression)
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<problems>
|
||||
<problem>
|
||||
<file>test.kt</file>
|
||||
<line>1</line>
|
||||
<module>light_idea_test_case</module>
|
||||
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
|
||||
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Remove redundant call to 'toString()' in string template</problem_class>
|
||||
<description>Redundant 'toString()' call in string template</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+1
@@ -0,0 +1 @@
|
||||
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.RemoveToStringInStringTemplateInspection
|
||||
@@ -0,0 +1 @@
|
||||
val z = "a${"b".toString()}"
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.RemoveToStringInStringTemplateInspection
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
operator fun Any.invoke() = this
|
||||
|
||||
fun foo(arg: Any) = "${arg().<caret>toString()}"
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
operator fun Any.invoke() = this
|
||||
|
||||
fun foo(arg: Any) = "${arg()}"
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
fun foo(arg: Any) = "arg = ${arg.<caret>toString()}"
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
fun foo(arg: Any) = "arg = $arg"
|
||||
@@ -0,0 +1,3 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
fun foo(arg: Any) = "arg = ${arg.<caret>toString()}xy"
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
fun foo(arg: Any) = "arg = ${arg}xy"
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
fun foo(s: String) = s
|
||||
|
||||
fun bar() = foo("a${"b".toString()<caret>}")
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Remove 'toString()' call" "true"
|
||||
|
||||
fun foo(s: String) = s
|
||||
|
||||
fun bar() = foo("a${"b"<caret>}")
|
||||
@@ -232,6 +232,12 @@ public class InspectionTestGenerated extends AbstractInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeToStringInStringTemplate/inspectionData/inspections.test")
|
||||
public void testRemoveToStringInStringTemplate_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/removeToStringInStringTemplate/inspectionData/inspections.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("replaceCallWithComparison/inspectionData/inspections.test")
|
||||
public void testReplaceCallWithComparison_inspectionData_Inspections_test() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspections/replaceCallWithComparison/inspectionData/inspections.test");
|
||||
|
||||
@@ -6830,6 +6830,39 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeToStringInStringTemplate")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RemoveToStringInStringTemplate extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInRemoveToStringInStringTemplate() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeToStringInStringTemplate"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("call.kt")
|
||||
public void testCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeToStringInStringTemplate/call.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("name.kt")
|
||||
public void testName() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeToStringInStringTemplate/name.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("nameWithPostfix.kt")
|
||||
public void testNameWithPostfix() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeToStringInStringTemplate/nameWithPostfix.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeToStringInStringTemplate/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeUnused")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user