Made convert to string template inspection less eager

This commit is contained in:
Valentin Kipyatkov
2015-09-03 22:18:39 +03:00
parent 67d1cc80f6
commit 97cf562dea
8 changed files with 12 additions and 106 deletions
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat
public class ConvertToStringTemplateInspection : IntentionBasedInspection<JetBinaryExpression>( public class ConvertToStringTemplateInspection : IntentionBasedInspection<JetBinaryExpression>(
ConvertToStringTemplateIntention(), ConvertToStringTemplateIntention(),
{ ConvertToStringTemplateIntention().isConversionResultSimple(it) } { ConvertToStringTemplateIntention().shouldSuggestToConvert(it) }
) )
public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Convert concatenation to template") { public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Convert concatenation to template") {
@@ -50,8 +50,9 @@ public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependen
return element.replaced(buildReplacement(element)) return element.replaced(buildReplacement(element))
} }
public fun isConversionResultSimple(expression: JetBinaryExpression): Boolean { public fun shouldSuggestToConvert(expression: JetBinaryExpression): Boolean {
return buildReplacement(expression).getEntries().none { it is JetBlockStringTemplateEntry } val entries = buildReplacement(expression).entries
return entries.none { it is JetBlockStringTemplateEntry } && entries.count { it is JetLiteralStringTemplateEntry } > 1
} }
private fun isApplicableToNoParentCheck(expression: JetBinaryExpression): Boolean { private fun isApplicableToNoParentCheck(expression: JetBinaryExpression): Boolean {
@@ -148,7 +148,7 @@ object J2KPostProcessingRegistrar {
private val intention = ConvertToStringTemplateIntention() private val intention = ConvertToStringTemplateIntention()
override fun createAction(element: JetElement, diagnostics: Diagnostics): (() -> Unit)? { override fun createAction(element: JetElement, diagnostics: Diagnostics): (() -> Unit)? {
if (element is JetBinaryExpression && intention.isApplicableTo(element) && intention.isConversionResultSimple(element)) { if (element is JetBinaryExpression && intention.isApplicableTo(element) && intention.shouldSuggestToConvert(element)) {
return { intention.applyTo(element) } return { intention.applyTo(element) }
} }
else { else {
@@ -1,20 +1,4 @@
<problems> <problems>
<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">Convert string concatenation 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">Convert string concatenation to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem> <problem>
<file>multilineString.kt</file> <file>multilineString.kt</file>
<line>3</line> <line>3</line>
@@ -23,78 +7,7 @@
<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>interpolateStringWithInt.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="interpolateStringWithInt.kt" />
<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>interpolateStringWithFloat.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="interpolateStringWithFloat.kt" />
<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>interpolateDollarSign.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="interpolateDollarSign.kt" />
<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>interpolateChar.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="interpolateChar.kt" />
<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>interpolate2Vals.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="interpolate2Vals.kt" />
<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>interpolate2StringConstants.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="interpolate2StringConstants.kt" />
<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>combinesNonStringsAsStrings2.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="combinesNonStringsAsStrings2.kt" />
<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>combinesNonStringsAsStrings.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="combinesNonStringsAsStrings.kt" />
<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>combineEmptyStrings.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="combineEmptyStrings.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert string concatenation to string template</problem_class>
<description>Convert concatenation to template</description>
</problem>
<problem> <problem>
<file>backslashNMultilineString.kt</file> <file>backslashNMultilineString.kt</file>
<line>2</line> <line>2</line>
@@ -103,12 +16,4 @@
<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>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">Convert string concatenation 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 }
+2 -2
View File
@@ -3,9 +3,9 @@ package demo
object Test { object Test {
fun subListRangeCheck(fromIndex: Int, toIndex: Int, size: Int) { fun subListRangeCheck(fromIndex: Int, toIndex: Int, size: Int) {
if (fromIndex < 0) if (fromIndex < 0)
throw IndexOutOfBoundsException("fromIndex = $fromIndex") throw IndexOutOfBoundsException("fromIndex = " + fromIndex)
if (toIndex > size) if (toIndex > size)
throw IndexOutOfBoundsException("toIndex = $toIndex") throw IndexOutOfBoundsException("toIndex = " + toIndex)
if (fromIndex > toIndex) if (fromIndex > toIndex)
throw IllegalArgumentException("fromIndex($fromIndex) > toIndex($toIndex)") throw IllegalArgumentException("fromIndex($fromIndex) > toIndex($toIndex)")
} }
+1 -1
View File
@@ -30,6 +30,6 @@ class A {
} }
public fun f(p: Int) { public fun f(p: Int) {
println("p = $p") println("p = " + p)
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ interface I {
open class A : I { 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)
} }
public fun foo(i: Int, c: Char) { public fun foo(i: Int, c: Char) {
+1 -1
View File
@@ -1,6 +1,6 @@
class A { class A {
JvmOverloads fun foo(i: Int, c: Char = 'a', s: String = "") { JvmOverloads fun foo(i: Int, c: Char = 'a', s: String = "") {
println("foo$i$c$s") println("foo" + i + c + s)
} }
JvmOverloads fun bar(s: String? = null): Int { JvmOverloads fun bar(s: String? = null): Int {