KT-13113: Add inspection to detect single-expression string template

(cherry picked from squashed commits 49a3ef3 and 4c7a42a)
This commit is contained in:
shiraji
2016-07-29 06:54:36 +03:00
committed by Mikhail Glukhikh
parent a9ee10a8b7
commit 9b8c55d823
18 changed files with 170 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports single-expression String template that can be safely removed
</body>
</html>
@@ -0,0 +1,4 @@
fun foo(x: Int, y: String) {
val z = y
val w = x.toString()
}
@@ -0,0 +1,4 @@
fun foo(x: Int, y: String) {
val z = "$y"
val w = "$x"
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention replaces single-expression string templates with expression itself, with toString() if necessary
</body>
</html>
+13
View File
@@ -1273,6 +1273,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -1644,6 +1649,14 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateInspection"
displayName="Remove redundant string template"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,53 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
private fun KtStringTemplateExpression.singleExpressionOrNull() =
children.singleOrNull()?.children?.firstOrNull() as? KtExpression
class RemoveSingleExpressionStringTemplateInspection : IntentionBasedInspection<KtStringTemplateExpression>(
RemoveSingleExpressionStringTemplateIntention()
) {
override val problemText = "Single-expression string template"
}
class RemoveSingleExpressionStringTemplateIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java,
"Remove single-expression string template"
) {
override fun isApplicableTo(element: KtStringTemplateExpression) =
element.singleExpressionOrNull() != null
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
val expression = element.singleExpressionOrNull() ?: return
val type = expression.getType(expression.analyze())
val newElement =
if (KotlinBuiltIns.isString(type)) expression
else KtPsiFactory(element).createExpressionByPattern("$0.$1()", expression, "toString")
element.replace(newElement)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateIntention
@@ -0,0 +1,3 @@
// IS_APPLICABLE: FALSE
val bar = <caret>""
@@ -0,0 +1,3 @@
// IS_APPLICABLE: FALSE
val bar = <caret>"Hello"
@@ -0,0 +1,4 @@
// IS_APPLICABLE: FALSE
val foo = "foo"
val bar = <caret>"$foo$foo"
@@ -0,0 +1,4 @@
// INTENTION_TEXT: Remove single-expression string template
val foo = "foo"
val bar = <caret>"$foo"
@@ -0,0 +1,4 @@
// INTENTION_TEXT: Remove single-expression string template
val foo = "foo"
val bar = foo
@@ -0,0 +1,3 @@
// INTENTION_TEXT: Remove single-expression string template
val bar = <caret>"${1.hashCode().toString()}"
@@ -0,0 +1,3 @@
// INTENTION_TEXT: Remove single-expression string template
val bar = 1.hashCode().toString()
@@ -0,0 +1,3 @@
// INTENTION_TEXT: Remove single-expression string template
val bar = <caret>"${1.hashCode()}"
@@ -0,0 +1,3 @@
// INTENTION_TEXT: Remove single-expression string template
val bar = 1.hashCode().toString()
@@ -0,0 +1,4 @@
// IS_APPLICABLE: FALSE
val foo = "foo"
val bar = <caret>"text$foo"
@@ -8480,6 +8480,57 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveSingleExpressionStringTemplate extends AbstractIntentionTest {
public void testAllFilesPresentInRemoveSingleExpressionStringTemplate() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeSingleExpressionStringTemplate"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("emptyString.kt")
public void testEmptyString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/emptyString.kt");
doTest(fileName);
}
@TestMetadata("justString.kt")
public void testJustString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/justString.kt");
doTest(fileName);
}
@TestMetadata("multipleStringTemplate.kt")
public void testMultipleStringTemplate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/multipleStringTemplate.kt");
doTest(fileName);
}
@TestMetadata("singleExpressionStringTemplate.kt")
public void testSingleExpressionStringTemplate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/singleExpressionStringTemplate.kt");
doTest(fileName);
}
@TestMetadata("singleExpressionStringTemplateWithMethodCall.kt")
public void testSingleExpressionStringTemplateWithMethodCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/singleExpressionStringTemplateWithMethodCall.kt");
doTest(fileName);
}
@TestMetadata("singleExpressionStringTemplateWithNonStringType.kt")
public void testSingleExpressionStringTemplateWithNonStringType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/singleExpressionStringTemplateWithNonStringType.kt");
doTest(fileName);
}
@TestMetadata("stringTemplateWithText.kt")
public void testStringTemplateWithText() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeSingleExpressionStringTemplate/stringTemplateWithText.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/removeUnnecessaryParentheses")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)