From b9b5e07820b2adc623a329d9f0d62b6cada34743 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sun, 6 Sep 2020 20:37:14 +0900 Subject: [PATCH] StringTemplateRule: don't report when template expression is keyword (#888) --- .../ktlint/ruleset/standard/StringTemplateRule.kt | 7 +++++++ .../test/resources/spec/string-template/lint.kt.spec | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/StringTemplateRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/StringTemplateRule.kt index 858a648899..27b779f99a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/StringTemplateRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/StringTemplateRule.kt @@ -9,6 +9,10 @@ import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY import com.pinterest.ktlint.core.ast.ElementType.SUPER_EXPRESSION import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement +import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry +import org.jetbrains.kotlin.psi.KtDotQualifiedExpression +import org.jetbrains.kotlin.psi.KtNameReferenceExpression +import org.jetbrains.kotlin.psi.KtThisExpression class StringTemplateRule : Rule("string-template") { @@ -30,6 +34,7 @@ class StringTemplateRule : Rule("string-template") { } */ if (elementType == LONG_STRING_TEMPLATE_ENTRY) { + var entryExpression = (node.psi as? KtBlockStringTemplateEntry)?.expression val entryStart = node.firstChildNode val dotQualifiedExpression = entryStart.treeNext if (dotQualifiedExpression?.elementType == DOT_QUALIFIED_EXPRESSION) { @@ -41,6 +46,7 @@ class StringTemplateRule : Rule("string-template") { ) { emit(dot.startOffset, "Redundant \"toString()\" call in string template", true) if (autoCorrect) { + entryExpression = (entryExpression as? KtDotQualifiedExpression)?.receiverExpression node.removeChild(dot) node.removeChild(callExpression) } @@ -48,6 +54,7 @@ class StringTemplateRule : Rule("string-template") { } if (node.text.startsWith("${'$'}{") && node.text.let { it.substring(2, it.length - 1) }.all { it.isPartOfIdentifier() } && + (entryExpression is KtNameReferenceExpression || entryExpression is KtThisExpression) && node.treeNext.let { nextSibling -> nextSibling.elementType == CLOSING_QUOTE || ( diff --git a/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec index 0ac10e805c..7e6cb5e352 100644 --- a/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec +++ b/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec @@ -50,6 +50,16 @@ class E { override fun toString(): String = "${s0}" } +class F { + fun keyword() { + println("${this}") + println("${this@F}") + println("${null}") + println("${true}") + println("${false}") + } +} + // expect // 2:29:Redundant "toString()" call in string template // 3:28:Redundant "toString()" call in string template @@ -57,3 +67,4 @@ class E { // 7:15:Redundant curly braces // 28:79:Redundant "toString()" call in string template // 45:20:Redundant curly braces +// 55:19:Redundant curly braces