diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt index c52a21f953c..21206b44006 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt @@ -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() + 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 diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt new file mode 100644 index 00000000000..16290de8cd6 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt @@ -0,0 +1,5 @@ +fun foo(vararg args: Int) {} + +fun test() { + foo(*intArrayOf()) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt.after new file mode 100644 index 00000000000..f6952d8bc18 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg args: Int) {} + +fun test() { + foo() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments2.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments2.kt new file mode 100644 index 00000000000..1e9ff8cfa20 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments2.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +fun foo(vararg args: Int) {} + +fun foo(vararg args: Double) {} + +fun test() { + foo(*intArrayOf()) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments3.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments3.kt new file mode 100644 index 00000000000..730c141b221 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOfWithoutArguments3.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +fun foo(vararg args: Int) {} + +fun foo() {} + +fun test() { + foo(*intArrayOf()) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 7daec00a368..8d28c1bd1c7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -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");