Redundant toString() in string template: highlight selector only
And not the receiver. So #KT-18253 Fixed
This commit is contained in:
+8
-7
@@ -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
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// PROBLEM: none
|
||||
|
||||
fun foo(a: Int, b: Int) = a + b
|
||||
|
||||
fun test(): String {
|
||||
return "Foo: ${<caret>foo(0, 4).toString()}"
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo(a: Int, b: Int) = a + b
|
||||
|
||||
fun test(): String {
|
||||
return "Foo: ${foo(0, 4).<caret>toString()}"
|
||||
}
|
||||
+5
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user