Redundant toString() in string template: highlight selector only

And not the receiver. So #KT-18253 Fixed
This commit is contained in:
Mikhail Glukhikh
2017-06-08 17:59:44 +03:00
parent 2afe8989df
commit 44e727b75c
6 changed files with 47 additions and 7 deletions
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
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
@@ -31,13 +30,15 @@ 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.receiverExpression is KtSuperExpression) return
if (!qualifiedExpression.isToString()) return
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
super.visitDotQualifiedExpression(expression)
holder.registerProblem(expression,
if (expression.parent !is KtBlockStringTemplateEntry) return
if (expression.receiverExpression is KtSuperExpression) return
val selectorExpression = expression.selectorExpression ?: return
if (!expression.isToString()) return
holder.registerProblem(selectorExpression,
"Redundant 'toString()' call in string template",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveToStringFix())
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.RemoveToStringInStringTemplateInspection
@@ -0,0 +1,7 @@
// PROBLEM: none
fun foo(a: Int, b: Int) = a + b
fun test(): String {
return "Foo: ${<caret>foo(0, 4).toString()}"
}
@@ -0,0 +1,5 @@
fun foo(a: Int, b: Int) = a + b
fun test(): String {
return "Foo: ${foo(0, 4).<caret>toString()}"
}
@@ -0,0 +1,5 @@
fun foo(a: Int, b: Int) = a + b
fun test(): String {
return "Foo: ${foo(0, 4)}"
}
@@ -147,6 +147,27 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/removeToStringInStringTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveToStringInStringTemplate extends AbstractLocalInspectionTest {
public void testAllFilesPresentInRemoveToStringInStringTemplate() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/removeToStringInStringTemplate"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("caretInReceiver.kt")
public void testCaretInReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeToStringInStringTemplate/caretInReceiver.kt");
doTest(fileName);
}
@TestMetadata("caretInSelector.kt")
public void testCaretInSelector() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeToStringInStringTemplate/caretInSelector.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceArrayOfWithLiteral")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)