From cbccf932a78d43e37d24a77f2ba178f866d383dc Mon Sep 17 00:00:00 2001 From: shiraji Date: Wed, 14 Jun 2017 04:52:22 +0300 Subject: [PATCH] Add inspection removing redundant spread operator for arrayOf call So #KT-17920 Fixed --- .../RemoveRedundantSpreadOperator.html | 5 + idea/src/META-INF/plugin.xml | 8 ++ ...RemoveRedundantSpreadOperatorInspection.kt | 74 ++++++++++++++ .../ReplaceArrayOfWithLiteralInspection.kt | 15 +-- .../jetbrains/kotlin/idea/intentions/Utils.kt | 15 +++ .../removeRedundantSpreadOperator/.inspection | 1 + .../removeRedundantSpreadOperator/basic.kt | 5 + .../basic.kt.after | 5 + .../booleanArrayOf.kt | 5 + .../booleanArrayOf.kt.after | 5 + .../byteArrayOf.kt | 5 + .../byteArrayOf.kt.after | 5 + .../charArrayOf.kt | 5 + .../charArrayOf.kt.after | 5 + .../doubleArrayOf.kt | 5 + .../doubleArrayOf.kt.after | 5 + .../emptyArray.kt | 5 + .../emptyArray.kt.after | 5 + .../floatArrayOf.kt | 5 + .../floatArrayOf.kt.after | 5 + .../intArrayOf.kt | 5 + .../intArrayOf.kt.after | 5 + .../longArrayOf.kt | 5 + .../longArrayOf.kt.after | 5 + .../multipleValues.kt | 5 + .../multipleValues.kt.after | 5 + .../multipleValuesWithOtherValues.kt | 5 + .../multipleValuesWithOtherValues.kt.after | 5 + .../namedArgument.kt | 8 ++ .../namedArgumentForArray.kt | 5 + .../namedArgumentForArray.kt.after | 5 + .../removeRedundantSpreadOperator/noParams.kt | 5 + .../noParams.kt.after | 5 + .../shortArrayOf.kt | 5 + .../shortArrayOf.kt.after | 5 + .../LocalInspectionTestGenerated.java | 99 +++++++++++++++++++ 36 files changed, 352 insertions(+), 13 deletions(-) create mode 100644 idea/resources/inspectionDescriptions/RemoveRedundantSpreadOperator.html create mode 100644 idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/.inspection create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgument.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt.after create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt create mode 100644 idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt.after diff --git a/idea/resources/inspectionDescriptions/RemoveRedundantSpreadOperator.html b/idea/resources/inspectionDescriptions/RemoveRedundantSpreadOperator.html new file mode 100644 index 00000000000..7265f2fb790 --- /dev/null +++ b/idea/resources/inspectionDescriptions/RemoveRedundantSpreadOperator.html @@ -0,0 +1,5 @@ + + +This inspection reports the redundant spread operator for arrayOf call + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 834c9c8c6e6..eb95a507353 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -2230,6 +2230,14 @@ language="kotlin" /> + + diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt new file mode 100644 index 00000000000..306308c47d5 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RemoveRedundantSpreadOperatorInspection.kt @@ -0,0 +1,74 @@ +/* + * 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.openapi.util.TextRange +import com.intellij.psi.PsiElementVisitor +import org.jetbrains.kotlin.idea.intentions.isArrayOfMethod +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 + +class RemoveRedundantSpreadOperatorInspection : AbstractKotlinInspection() { + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { + return object : KtVisitorVoid() { + override fun visitArgument(argument: KtValueArgument) { + super.visitArgument(argument) + val spreadElement = argument.getSpreadElement() ?: return + if (argument.isNamed()) return + val argumentExpression = argument.getArgumentExpression() as? KtCallExpression ?: return + if (argumentExpression.isArrayOfMethod()) { + val argumentOffset = argument.startOffset + val problemDescriptor = holder.manager.createProblemDescriptor( + argument, + TextRange(spreadElement.startOffset - argumentOffset, + argumentExpression.calleeExpression!!.endOffset - argumentOffset), + "Remove redundant spread operator", + ProblemHighlightType.LIKE_UNUSED_SYMBOL, + isOnTheFly, + RemoveRedundantSpreadOperatorQuickfix() + ) + holder.registerProblem(problemDescriptor) + } + } + } + } +} + +class RemoveRedundantSpreadOperatorQuickfix : LocalQuickFix { + override fun getFamilyName() = "Remove redundant spread operator" + + override fun applyFix(project: Project, descriptor: ProblemDescriptor) { + val arrayOfValueArgument = descriptor.psiElement as? KtValueArgument ?: return + val arrayOfArgumentExpression = arrayOfValueArgument.getArgumentExpression() as? KtCallExpression ?: return + val arrayOfArgumentList = arrayOfArgumentExpression.valueArgumentList ?: return + val factory = KtPsiFactory(project) + arrayOfValueArgument.getStrictParentOfType()?.let { outerArgumentList -> + arrayOfArgumentList.arguments.reversed().forEach { argument -> + val newValueArgument = factory.createArgument(argument.getArgumentExpression()) + outerArgumentList.addArgumentAfter(newValueArgument, arrayOfValueArgument) + } + outerArgumentList.removeArgument(arrayOfValueArgument) + } + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt index 3de9d12f9de..36701c19182 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayOfWithLiteralInspection.kt @@ -23,21 +23,13 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.project.Project import com.intellij.psi.PsiElementVisitor -import org.jetbrains.kotlin.builtins.BuiltInsPackageFragment import org.jetbrains.kotlin.config.LanguageFeature.ArrayLiteralsInAnnotations -import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.intentions.isArrayOfMethod import org.jetbrains.kotlin.idea.project.languageVersionSettings -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.CollectionLiteralResolver -import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall class ReplaceArrayOfWithLiteralInspection : AbstractKotlinInspection() { - private val acceptableNames = setOf(CollectionLiteralResolver.ARRAY_OF_FUNCTION) + - CollectionLiteralResolver.PRIMITIVE_TYPE_TO_ARRAY.values.toSet() + - Name.identifier("emptyArray") - override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { return object : KtVisitorVoid() { override fun visitCallExpression(expression: KtCallExpression) { @@ -47,10 +39,7 @@ class ReplaceArrayOfWithLiteralInspection : AbstractKotlinInspection() { !ApplicationManager.getApplication().isUnitTestMode) return val calleeExpression = expression.calleeExpression as? KtNameReferenceExpression ?: return - val resolvedCall = expression.getResolvedCall(expression.analyze()) ?: return - val descriptor = resolvedCall.candidateDescriptor - if (descriptor.containingDeclaration !is BuiltInsPackageFragment) return - if (descriptor.name !in acceptableNames) return + if (!expression.isArrayOfMethod()) return val parent = expression.parent when (parent) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index 68774851235..4feaf2a78e7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.kotlin.builtins.BuiltInsPackageFragment import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor @@ -26,10 +27,14 @@ import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.setType import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.CollectionLiteralResolver import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.KotlinType @@ -272,3 +277,13 @@ fun KtDotQualifiedExpression.deleteFirstReceiver(): KtExpression { } return this } + +private val ARRAY_OF_METHODS = setOf(CollectionLiteralResolver.ARRAY_OF_FUNCTION) + + CollectionLiteralResolver.PRIMITIVE_TYPE_TO_ARRAY.values.toSet() + + Name.identifier("emptyArray") + +fun KtCallExpression.isArrayOfMethod(): Boolean { + val resolvedCall = getResolvedCall(analyze()) ?: return false + val descriptor = resolvedCall.candidateDescriptor + return descriptor.containingDeclaration is BuiltInsPackageFragment && ARRAY_OF_METHODS.contains(descriptor.name) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/.inspection b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/.inspection new file mode 100644 index 00000000000..b60189120a6 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.RemoveRedundantSpreadOperatorInspection \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt new file mode 100644 index 00000000000..70a5c07313e --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo(*arrayOf("abc")) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt.after new file mode 100644 index 00000000000..117120e65f4 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo("abc") +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt new file mode 100644 index 00000000000..3107b73896e --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Boolean) {} + +fun bar() { + foo(*booleanArrayOf(true, true)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt.after new file mode 100644 index 00000000000..57f630141b3 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Boolean) {} + +fun bar() { + foo(true, true) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt new file mode 100644 index 00000000000..19f5b7c96ef --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Byte) {} + +fun bar() { + foo(*byteArrayOf(1)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt.after new file mode 100644 index 00000000000..7a5b98afcc1 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Byte) {} + +fun bar() { + foo(1) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt new file mode 100644 index 00000000000..c15915c818d --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Char) {} + +fun bar() { + foo(*charArrayOf('a', 'b')) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt.after new file mode 100644 index 00000000000..08d19d42cb2 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Char) {} + +fun bar() { + foo('a', 'b') +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt new file mode 100644 index 00000000000..90dabad7a9a --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Double) {} + +fun bar() { + foo(*doubleArrayOf(1.0)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt.after new file mode 100644 index 00000000000..82c0430c1c7 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Double) {} + +fun bar() { + foo(1.0) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt new file mode 100644 index 00000000000..390fdf9754b --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo(*emptyArray()) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt.after new file mode 100644 index 00000000000..b46dfee5cb6 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo() +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt new file mode 100644 index 00000000000..deef891a6ca --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Float) {} + +fun bar() { + foo(*floatArrayOf(1.0f)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt.after new file mode 100644 index 00000000000..4a829a07227 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Float) {} + +fun bar() { + foo(1.0f) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt new file mode 100644 index 00000000000..0f38779a460 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Int) {} + +fun bar() { + foo(*intArrayOf(1)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt.after new file mode 100644 index 00000000000..5f7d272a40d --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Int) {} + +fun bar() { + foo(1) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt new file mode 100644 index 00000000000..391c330f745 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Long) {} + +fun bar() { + foo(*longArrayOf(1L)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt.after new file mode 100644 index 00000000000..7bf65db9530 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Long) {} + +fun bar() { + foo(1L) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt new file mode 100644 index 00000000000..cd348b7db30 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo(*arrayOf("abc", "def")) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt.after new file mode 100644 index 00000000000..cb992c67175 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo("abc", "def") +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt new file mode 100644 index 00000000000..bb0119420e1 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo(*arrayOf("abc", "def"), "ghi", "jkl") +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt.after new file mode 100644 index 00000000000..aedebbd2298 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo("abc", "def", "ghi", "jkl") +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgument.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgument.kt new file mode 100644 index 00000000000..6d857219973 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgument.kt @@ -0,0 +1,8 @@ +// PROBLEM: none + +fun foo(vararg x: String) {} + +fun bar() { + // We do not want to apply inspection here, because named argument is lost + foo(x = *arrayOf("abc")) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt new file mode 100644 index 00000000000..035bbf2a92c --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo(*arrayOf(elements = "abc")) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt.after new file mode 100644 index 00000000000..117120e65f4 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo("abc") +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt new file mode 100644 index 00000000000..e04d85bf7d9 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo(*arrayOf()) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt.after new file mode 100644 index 00000000000..b46dfee5cb6 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: String) {} + +fun bar() { + foo() +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt new file mode 100644 index 00000000000..6c537271428 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt @@ -0,0 +1,5 @@ +fun foo(vararg x: Short) {} + +fun bar() { + foo(*shortArrayOf(1)) +} diff --git a/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt.after b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt.after new file mode 100644 index 00000000000..e9521c68243 --- /dev/null +++ b/idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt.after @@ -0,0 +1,5 @@ +fun foo(vararg x: Short) {} + +fun bar() { + foo(1) +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 213f8a41d92..7a95a03aae9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -216,6 +216,105 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveRedundantSpreadOperator extends AbstractLocalInspectionTest { + public void testAllFilesPresentInRemoveRedundantSpreadOperator() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/removeRedundantSpreadOperator"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("basic.kt") + public void testBasic() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/basic.kt"); + doTest(fileName); + } + + @TestMetadata("booleanArrayOf.kt") + public void testBooleanArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/booleanArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("byteArrayOf.kt") + public void testByteArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/byteArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("charArrayOf.kt") + public void testCharArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/charArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("doubleArrayOf.kt") + public void testDoubleArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/doubleArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("emptyArray.kt") + public void testEmptyArray() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/emptyArray.kt"); + doTest(fileName); + } + + @TestMetadata("floatArrayOf.kt") + public void testFloatArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/floatArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("intArrayOf.kt") + public void testIntArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/intArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("longArrayOf.kt") + public void testLongArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/longArrayOf.kt"); + doTest(fileName); + } + + @TestMetadata("multipleValues.kt") + public void testMultipleValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValues.kt"); + doTest(fileName); + } + + @TestMetadata("multipleValuesWithOtherValues.kt") + public void testMultipleValuesWithOtherValues() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/multipleValuesWithOtherValues.kt"); + doTest(fileName); + } + + @TestMetadata("namedArgument.kt") + public void testNamedArgument() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgument.kt"); + doTest(fileName); + } + + @TestMetadata("namedArgumentForArray.kt") + public void testNamedArgumentForArray() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/namedArgumentForArray.kt"); + doTest(fileName); + } + + @TestMetadata("noParams.kt") + public void testNoParams() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/noParams.kt"); + doTest(fileName); + } + + @TestMetadata("shortArrayOf.kt") + public void testShortArrayOf() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/removeRedundantSpreadOperator/shortArrayOf.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/inspectionsLocal/removeToStringInStringTemplate") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)