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 { fun shouldSuggestToConvert(expression: KtBinaryExpression): Boolean {
val entries = buildReplacement(expression).entries val entries = buildReplacement(expression).entries
return entries.none { it is KtBlockStringTemplateEntry } return entries.none { it is KtBlockStringTemplateEntry }
&& !entries.all { it is KtLiteralStringTemplateEntry || it is KtEscapeStringTemplateEntry } && !entries.all { it is KtLiteralStringTemplateEntry || it is KtEscapeStringTemplateEntry }
&& entries.count { it is KtLiteralStringTemplateEntry } > 1 && entries.count { it is KtLiteralStringTemplateEntry } >= 1
&& !expression.textContains('\n') && !expression.textContains('\n')
} }
@JvmStatic @JvmStatic
@@ -7,4 +7,44 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert string concatenation to string template</problem_class> <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert string concatenation to string template</problem_class>
<description>Convert concatenation to template</description> <description>Convert concatenation to template</description>
</problem> </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> </problems>
@@ -1 +1 @@
assert(true) { "string details:" + x } assert(true) { "string details:$x" }
@@ -11,6 +11,6 @@ class C {
} }
internal fun foo() { internal fun foo() {
println("myX = " + myX) println("myX = $myX")
} }
} }
@@ -11,6 +11,6 @@ class C {
} }
internal fun foo() { internal fun foo() {
println("x = " + x) println("x = $x")
} }
} }
+3 -8
View File
@@ -2,13 +2,8 @@
package demo; package demo;
class Test { class Test {
static void subListRangeCheck(int fromIndex, int toIndex, int size) { static void bar(int a) {
if (fromIndex < 0) if (a < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); throw new RuntimeException("a = " + a);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex
+ ") > toIndex(" + toIndex + ")");
} }
} }
+3 -8
View File
@@ -1,13 +1,8 @@
package demo package demo
internal object Test { internal object Test {
fun subListRangeCheck(fromIndex: Int, toIndex: Int, size: Int) { fun bar(a: Int) {
if (fromIndex < 0) if (a < 0)
throw IndexOutOfBoundsException("fromIndex = " + fromIndex) throw RuntimeException("a = $a")
if (toIndex > size)
throw IndexOutOfBoundsException("toIndex = " + toIndex)
if (fromIndex > toIndex)
throw IllegalArgumentException("fromIndex(" + fromIndex
+ ") > toIndex(" + toIndex + ")")
} }
} }
@@ -89,7 +89,7 @@ internal class A {
val limit = 5 val limit = 5
useSplit(s.split("\\s+".toRegex(), limit.coerceAtLeast(0)).toTypedArray()) useSplit(s.split("\\s+".toRegex(), limit.coerceAtLeast(0)).toTypedArray())
s.trim { it <= ' ' } s.trim { it <= ' ' }
s + " another" "$s another"
s.toByteArray() s.toByteArray()
s.toByteArray(Charset.forName("utf-8")) s.toByteArray(Charset.forName("utf-8"))
+1 -1
View File
@@ -31,6 +31,6 @@ internal class A {
} }
fun f(p: Int) { fun f(p: Int) {
println("p = " + p) println("p = $p")
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ internal interface I {
internal open class A : I { internal open class A : I {
override fun foo(i: Int, c: Char, s: String) { 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) { fun foo(i: Int, c: Char) {
+1 -1
View File
@@ -1,7 +1,7 @@
internal class A { internal class A {
@JvmOverloads @JvmOverloads
fun foo(i: Int, c: Char = 'a', s: String = "") { fun foo(i: Int, c: Char = 'a', s: String = "") {
println("foo" + i + c + s) println("foo$i$c$s")
} }
@JvmOverloads @JvmOverloads