Propose convert to string template as inspection more frequent

This commit is contained in:
Simon Ogorodnik
2018-02-22 15:10:47 +03:00
committed by Stanislav Erokhin
parent 4c15642e2c
commit 57e47d1830
11 changed files with 60 additions and 30 deletions
@@ -51,9 +51,9 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
fun shouldSuggestToConvert(expression: KtBinaryExpression): Boolean {
val entries = buildReplacement(expression).entries
return entries.none { it is KtBlockStringTemplateEntry }
&& !entries.all { it is KtLiteralStringTemplateEntry || it is KtEscapeStringTemplateEntry }
&& entries.count { it is KtLiteralStringTemplateEntry } > 1
&& !expression.textContains('\n')
&& !entries.all { it is KtLiteralStringTemplateEntry || it is KtEscapeStringTemplateEntry }
&& entries.count { it is KtLiteralStringTemplateEntry } >= 1
&& !expression.textContains('\n')
}
@JvmStatic
@@ -7,4 +7,44 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert string concatenation to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem>
<file>noBracesSimpleFollowedByDot.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="noBracesSimpleFollowedByDot.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">String concatenation that can be converted to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem>
<file>specialCharsInCharLiteral.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="specialCharsInCharLiteral.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">String concatenation that can be converted to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem>
<file>doesNotCorruptExistingTemplate.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="doesNotCorruptExistingTemplate.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">String concatenation that can be converted to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem>
<file>unescapeSingleQuote.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="unescapeSingleQuote.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">String concatenation that can be converted to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem>
<file>noBracesForLastSimpleExpression.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="noBracesForLastSimpleExpression.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">String concatenation that can be converted to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
</problems>
@@ -1 +1 @@
assert(true) { "string details:" + x }
assert(true) { "string details:$x" }
@@ -11,6 +11,6 @@ class C {
}
internal fun foo() {
println("myX = " + myX)
println("myX = $myX")
}
}
@@ -11,6 +11,6 @@ class C {
}
internal fun foo() {
println("x = " + x)
println("x = $x")
}
}
}
+3 -8
View File
@@ -2,13 +2,8 @@
package demo;
class Test {
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex
+ ") > toIndex(" + toIndex + ")");
static void bar(int a) {
if (a < 0)
throw new RuntimeException("a = " + a);
}
}
+3 -8
View File
@@ -1,13 +1,8 @@
package demo
internal object Test {
fun subListRangeCheck(fromIndex: Int, toIndex: Int, size: Int) {
if (fromIndex < 0)
throw IndexOutOfBoundsException("fromIndex = " + fromIndex)
if (toIndex > size)
throw IndexOutOfBoundsException("toIndex = " + toIndex)
if (fromIndex > toIndex)
throw IllegalArgumentException("fromIndex(" + fromIndex
+ ") > toIndex(" + toIndex + ")")
fun bar(a: Int) {
if (a < 0)
throw RuntimeException("a = $a")
}
}
@@ -89,7 +89,7 @@ internal class A {
val limit = 5
useSplit(s.split("\\s+".toRegex(), limit.coerceAtLeast(0)).toTypedArray())
s.trim { it <= ' ' }
s + " another"
"$s another"
s.toByteArray()
s.toByteArray(Charset.forName("utf-8"))
+2 -2
View File
@@ -31,6 +31,6 @@ internal class A {
}
fun f(p: Int) {
println("p = " + p)
println("p = $p")
}
}
}
+2 -2
View File
@@ -5,7 +5,7 @@ internal interface I {
internal open class A : I {
override fun foo(i: Int, c: Char, s: String) {
println("foo" + i + c + s)
println("foo$i$c$s")
}
fun foo(i: Int, c: Char) {
@@ -43,4 +43,4 @@ internal class B : A() {
override fun y(i: Int) {
super.y(i)
}
}
}
+2 -2
View File
@@ -1,7 +1,7 @@
internal class A {
@JvmOverloads
fun foo(i: Int, c: Char = 'a', s: String = "") {
println("foo" + i + c + s)
println("foo$i$c$s")
}
@JvmOverloads
@@ -9,4 +9,4 @@ internal class A {
println("s = " + s!!)
return 0
}
}
}