code style inspection: to -> Pair function used not in infix form

This commit is contained in:
kenji tomita
2017-12-12 21:38:17 +09:00
committed by Dmitry Jemerov
parent 41739602bc
commit 37351c344f
8 changed files with 130 additions and 2 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports <code>to</code> function calls replaceable with the infix form
</body>
</html>
+10 -2
View File
@@ -2683,6 +2683,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceToWithInfixFormInspection"
displayName="Replace to with infix form"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="WEAK WARNING"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplacePutWithAssignmentInspection"
displayName="map.put() can be converted to assignment"
groupPath="Kotlin"
@@ -2700,8 +2709,7 @@
level="WARNING"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.SortModifiersInspection"
displayName="Non-canonical modifier order"
groupPath="Kotlin"
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2017 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.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.calleeName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class ReplaceToWithInfixFormInspection : AbstractKotlinInspection() {
private val compatibleNames = setOf("to")
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : KtVisitorVoid() {
override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression) {
super.visitDotQualifiedExpression(expression)
if (expression.callExpression?.valueArguments?.size != 1) return
if (expression.calleeName !in compatibleNames) return
val context = expression.analyze(BodyResolveMode.FULL)
val resolvedCall = expression.getResolvedCall(context) ?: return
val function = resolvedCall.resultingDescriptor as? FunctionDescriptor ?: return
if (!function.isInfix) return
holder.registerProblem(
expression,
"Replace 'to' with infix form",
ProblemHighlightType.WEAK_WARNING,
ReplaceToWithInfixFormQuickfix()
)
}
}
}
}
class ReplaceToWithInfixFormQuickfix : LocalQuickFix {
override fun getName() = "Replace 'to' with infix form"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement as KtDotQualifiedExpression
element.replace(KtPsiFactory(element).createExpressionByPattern("$0 to $1", element.receiverExpression,
element.callExpression?.valueArguments?.get(0)?.getArgumentExpression() ?: return))
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ReplaceToWithInfixFormInspection
@@ -0,0 +1,7 @@
// WITH_RUNTIME
class A
class B
fun foo(a: A, b: B) {
val pair = a.<caret>to(b)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
class A
class B
fun foo(a: A, b: B) {
val pair = a to b
}
@@ -0,0 +1,9 @@
// PROBLEM: none
class A {
fun to(x: Int) {
}
}
fun foo() {
A().<caret>to(1)
}
@@ -2463,6 +2463,27 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceToWithInfixForm")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceToWithInfixForm extends AbstractLocalInspectionTest {
public void testAllFilesPresentInReplaceToWithInfixForm() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceToWithInfixForm"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("base.kt")
public void testBase() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceToWithInfixForm/base.kt");
doTest(fileName);
}
@TestMetadata("nonPair.kt")
public void testNonPair() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/replaceToWithInfixForm/nonPair.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/selfAssignment")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)