Add inspection (INFORMATION) to convert Pair constructor into to call

So #KT-22933 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-02-28 17:55:05 +03:00
committed by Mikhail Glukhikh
parent 963e430b42
commit f48eacfbd4
8 changed files with 107 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports Pair constructor that can be converted to 'to' function.
</body>
</html>
+9
View File
@@ -2734,6 +2734,15 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ConvertPairConstructorToToFunctionInspection"
displayName="Convert Pair constructor to 'to' function"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,53 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
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.idea.intentions.getCallableDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.callExpressionVisitor
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertPairConstructorToToFunctionInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return callExpressionVisitor { expression ->
if (expression.isPairConstructorCall()) {
holder.registerProblem(
expression,
"Can be converted to 'to'",
ProblemHighlightType.INFORMATION,
ConvertPairConstructorToToFix()
)
}
}
}
}
private class ConvertPairConstructorToToFix : LocalQuickFix {
override fun getName() = "Convert to 'to'"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val expression = descriptor.psiElement as? KtCallExpression ?: return
val args = expression.valueArguments.mapNotNull { it.getArgumentExpression() }.toTypedArray()
expression.replace(KtPsiFactory(expression).createExpressionByPattern("$0 to $1", *args))
}
}
private fun KtCallExpression.isPairConstructorCall(): Boolean {
if (valueArguments.size != 2) return false
if (valueArguments.mapNotNull { it.getArgumentExpression() }.size != 2) return false
return getCallableDescriptor()?.containingDeclaration?.fqNameSafe == FqName("kotlin.Pair")
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.ConvertPairConstructorToToFunctionInspection
@@ -0,0 +1,8 @@
// PROBLEM: none
// WITH_RUNTIME
// ERROR: No value passed for parameter 'second'
// ERROR: Type inference failed: Not enough information to infer parameter B in constructor Pair<out A, out B>(first: A, second: B)<br>Please specify it explicitly.<br>
import kotlin.Pair
fun test() {
val p = <caret>Pair(1, )
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
import kotlin.Pair
fun test() {
val p = <caret>Pair(1, "foo")
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
import kotlin.Pair
fun test() {
val p = 1 to "foo"
}
@@ -1213,6 +1213,27 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/convertPairConstructorToToFunction")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertPairConstructorToToFunction extends AbstractLocalInspectionTest {
public void testAllFilesPresentInConvertPairConstructorToToFunction() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/convertPairConstructorToToFunction"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("invalidArgs.kt")
public void testInvalidArgs() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertPairConstructorToToFunction/invalidArgs.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/convertPairConstructorToToFunction/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/copyWithoutNamedArguments")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)