Remove redundant spread operator: don't report if overloaded functions cannot be resolved

#KT-38282 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-04-21 17:29:49 +09:00
committed by Dmitry Gridin
parent f179b39c70
commit 0a230905ae
6 changed files with 58 additions and 0 deletions
@@ -13,11 +13,16 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.isArrayOfMethod
import org.jetbrains.kotlin.idea.refactoring.replaceWithCopyWithResolveCheck
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class RemoveRedundantSpreadOperatorInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
@@ -31,6 +36,18 @@ class RemoveRedundantSpreadOperatorInspection : AbstractKotlinInspection() {
when (argumentExpression) {
is KtCallExpression -> {
if (!argumentExpression.isArrayOfMethod()) return
if (argumentExpression.valueArguments.isEmpty()) {
val call = argument.getStrictParentOfType<KtCallExpression>()
if (call != null) {
val bindingContext = call.analyze(BodyResolveMode.PARTIAL)
if (call.replaceWithCopyWithResolveCheck(
resolveStrategy = { expr, context -> expr.getResolvedCall(context)?.resultingDescriptor },
context = bindingContext,
preHook = { valueArgumentList?.removeArgument(call.valueArguments.indexOfFirst { it == argument }) }
) == null
) return
}
}
argumentExpression.calleeExpression!!.endOffset - argumentOffset
}
is KtCollectionLiteralExpression -> startOffset + 1
@@ -0,0 +1,5 @@
fun foo(vararg args: Int) {}
fun test() {
foo(<caret>*intArrayOf())
}
@@ -0,0 +1,5 @@
fun foo(vararg args: Int) {}
fun test() {
foo()
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun foo(vararg args: Int) {}
fun foo(vararg args: Double) {}
fun test() {
foo(<caret>*intArrayOf())
}
@@ -0,0 +1,8 @@
// PROBLEM: none
fun foo(vararg args: Int) {}
fun foo() {}
fun test() {
foo(<caret>*intArrayOf())
}
@@ -9727,6 +9727,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt");
}
@TestMetadata("intArrayOfWithoutArguments.kt")
public void testIntArrayOfWithoutArguments() throws Exception {
runTest("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt");
}
@TestMetadata("intArrayOfWithoutArguments2.kt")
public void testIntArrayOfWithoutArguments2() throws Exception {
runTest("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments2.kt");
}
@TestMetadata("intArrayOfWithoutArguments3.kt")
public void testIntArrayOfWithoutArguments3() throws Exception {
runTest("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments3.kt");
}
@TestMetadata("literal.kt")
public void testLiteral() throws Exception {
runTest("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/literal.kt");