Prohibit assigning single elements into varargs in named form
#KT-20588 Fixed #KT-20589 Fixed
This commit is contained in:
@@ -664,7 +664,9 @@ public interface Errors {
|
||||
DiagnosticFactory0<KtExpression> NO_RECEIVER_ALLOWED = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
DiagnosticFactory1<KtExpression, KotlinType> ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION = DiagnosticFactory1.create(WARNING);
|
||||
DiagnosticFactory1<KtExpression, KotlinType> ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory0<KtExpression> ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION = DiagnosticFactory0.create(WARNING);
|
||||
DiagnosticFactory0<KtExpression> ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION_ERROR = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
// Call resolution
|
||||
|
||||
|
||||
+2
@@ -780,7 +780,9 @@ public class DefaultErrorMessages {
|
||||
MAP.put(MISSING_RECEIVER, "A receiver of type {0} is required", RENDER_TYPE);
|
||||
MAP.put(NO_RECEIVER_ALLOWED, "No receiver can be passed to this function or property");
|
||||
MAP.put(ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION, "Assigning single elements to varargs in named form is deprecated", TO_STRING);
|
||||
MAP.put(ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR, "Assigning single elements to varargs in named form is forbidden", TO_STRING);
|
||||
MAP.put(ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION, "Assigning single elements to varargs in named form is deprecated");
|
||||
MAP.put(ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION_ERROR, "Assigning single elements to varargs in named form is forbidden");
|
||||
|
||||
MAP.put(CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS, "Cannot create an instance of an abstract class");
|
||||
|
||||
|
||||
+47
-11
@@ -6,9 +6,13 @@
|
||||
package org.jetbrains.kotlin.resolve.calls.checkers
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageFeature.AssigningArraysToVarargsInNamedFormInAnnotations
|
||||
import org.jetbrains.kotlin.config.LanguageFeature.ProhibitAssigningSingleElementsToVarargsInNamedForm
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION
|
||||
import org.jetbrains.kotlin.diagnostics.Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.ValueArgument
|
||||
import org.jetbrains.kotlin.resolve.calls.callResolverUtil.isArrayOrArrayLiteral
|
||||
@@ -18,6 +22,18 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isParameterOfAnnotation
|
||||
|
||||
class AssigningNamedArgumentToVarargChecker : CallChecker {
|
||||
companion object {
|
||||
private val migrationDiagnosticsForFunction = MigrationDiagnostics(
|
||||
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION,
|
||||
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR
|
||||
)
|
||||
|
||||
private val migrationDiagnosticsForAnnotation = MigrationDiagnostics(
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION,
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION_ERROR
|
||||
)
|
||||
}
|
||||
|
||||
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
for ((parameterDescriptor, resolvedArgument) in resolvedCall.valueArguments) {
|
||||
for (argument in resolvedArgument.arguments) {
|
||||
@@ -31,7 +47,7 @@ class AssigningNamedArgumentToVarargChecker : CallChecker {
|
||||
parameterDescriptor: ValueParameterDescriptor,
|
||||
context: ResolutionContext<*>
|
||||
) {
|
||||
if (!context.languageVersionSettings.supportsFeature(LanguageFeature.AssigningArraysToVarargsInNamedFormInAnnotations)) return
|
||||
if (!context.languageVersionSettings.supportsFeature(AssigningArraysToVarargsInNamedFormInAnnotations)) return
|
||||
|
||||
if (!argument.isNamed()) return
|
||||
if (!parameterDescriptor.isVararg) return
|
||||
@@ -50,12 +66,19 @@ class AssigningNamedArgumentToVarargChecker : CallChecker {
|
||||
argumentExpression: KtExpression,
|
||||
context: ResolutionContext<*>
|
||||
) {
|
||||
fun report(onlyWarning: Boolean = false) {
|
||||
reportMigrationDiagnostic(migrationDiagnosticsForAnnotation, context, onlyWarning) { diagnostic ->
|
||||
context.trace.report(diagnostic.on(argumentExpression))
|
||||
}
|
||||
}
|
||||
|
||||
if (isArrayOrArrayLiteral(argument, context.trace)) {
|
||||
if (argument.hasSpread()) {
|
||||
context.trace.report(Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION.on(argumentExpression))
|
||||
// We want to make calls @Foo(value = [A]) and @Foo(value = *[A]) equivalent
|
||||
report(onlyWarning = true)
|
||||
}
|
||||
} else {
|
||||
context.trace.report(Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION.on(argumentExpression))
|
||||
report()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,14 +89,27 @@ class AssigningNamedArgumentToVarargChecker : CallChecker {
|
||||
parameterDescriptor: ValueParameterDescriptor
|
||||
) {
|
||||
if (!argument.hasSpread()) {
|
||||
context.trace.report(
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION.on(
|
||||
argumentExpression,
|
||||
parameterDescriptor.type
|
||||
)
|
||||
)
|
||||
reportMigrationDiagnostic(migrationDiagnosticsForFunction, context) { diagnostic ->
|
||||
context.trace.report(diagnostic.on(argumentExpression, parameterDescriptor.type))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ValueArgument.hasSpread() = getSpreadElement() != null
|
||||
}
|
||||
|
||||
private inline fun <T : DiagnosticFactory<*>> reportMigrationDiagnostic(
|
||||
migrationDiagnostics: MigrationDiagnostics<T>,
|
||||
context: ResolutionContext<*>,
|
||||
onlyWarning: Boolean = false,
|
||||
report: (T) -> Unit
|
||||
) {
|
||||
val (warning, error) = migrationDiagnostics
|
||||
if (!onlyWarning && context.languageVersionSettings.supportsFeature(ProhibitAssigningSingleElementsToVarargsInNamedForm)) {
|
||||
report(error)
|
||||
} else {
|
||||
report(warning)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class MigrationDiagnostics<T : DiagnosticFactory<*>>(val warning: T, val error: T)
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// !LANGUAGE: +ProhibitAssigningSingleElementsToVarargsInNamedForm
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
annotation class Anno1(vararg val s: String)
|
||||
annotation class Anno2(vararg val i: Int)
|
||||
|
||||
@Anno1(s = <!ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION_ERROR!>"foo"<!>)
|
||||
@Anno2(i = *<!ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION!>intArrayOf(1)<!>)
|
||||
fun f1() {}
|
||||
|
||||
@Anno1(s = ["foo"])
|
||||
@Anno2(i = intArrayOf(1))
|
||||
fun f2() {}
|
||||
|
||||
fun foo(vararg ints: Int) {}
|
||||
|
||||
fun test() {
|
||||
foo(ints = <!ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR!>1<!>)
|
||||
foo(ints = *intArrayOf(1))
|
||||
}
|
||||
@@ -22130,6 +22130,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
runTest("compiler/testData/diagnostics/tests/varargs/NullableTypeForVarargArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("prohibitAssigningSingleElementsInNamedForm.kt")
|
||||
public void testProhibitAssigningSingleElementsInNamedForm() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/varargs/prohibitAssigningSingleElementsInNamedForm.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("UnaryVsVararg.kt")
|
||||
public void testUnaryVsVararg() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/varargs/UnaryVsVararg.kt");
|
||||
|
||||
Generated
+5
@@ -22130,6 +22130,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
|
||||
runTest("compiler/testData/diagnostics/tests/varargs/NullableTypeForVarargArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("prohibitAssigningSingleElementsInNamedForm.kt")
|
||||
public void testProhibitAssigningSingleElementsInNamedForm() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/varargs/prohibitAssigningSingleElementsInNamedForm.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("UnaryVsVararg.kt")
|
||||
public void testUnaryVsVararg() throws Exception {
|
||||
runTest("compiler/testData/diagnostics/tests/varargs/UnaryVsVararg.kt");
|
||||
|
||||
@@ -77,6 +77,7 @@ enum class LanguageFeature(
|
||||
ProhibitLocalAnnotations(KOTLIN_1_3, kind = BUG_FIX),
|
||||
ProhibitSmartcastsOnLocalDelegatedProperty(KOTLIN_1_3, kind = BUG_FIX),
|
||||
ProhibitOperatorMod(KOTLIN_1_3, kind = BUG_FIX),
|
||||
ProhibitAssigningSingleElementsToVarargsInNamedForm(KOTLIN_1_3, kind = BUG_FIX),
|
||||
|
||||
StrictJavaNullabilityAssertions(sinceVersion = null, defaultState = State.DISABLED),
|
||||
ProperIeee754Comparisons(sinceVersion = null, defaultState = State.DISABLED),
|
||||
|
||||
@@ -545,7 +545,9 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
ANNOTATION_USED_AS_ANNOTATION_ARGUMENT.registerFactory(RemoveAtFromAnnotationArgument)
|
||||
|
||||
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION.registerFactory(ReplaceWithArrayCallInAnnotationFix)
|
||||
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_ANNOTATION_ERROR.registerFactory(ReplaceWithArrayCallInAnnotationFix)
|
||||
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION.registerFactory(SurroundWithArrayOfWithSpreadOperatorInFunctionFix)
|
||||
ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR.registerFactory(SurroundWithArrayOfWithSpreadOperatorInFunctionFix)
|
||||
|
||||
JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE.registerFactory(KotlinAddRequiredModuleFix)
|
||||
|
||||
|
||||
+10
-1
@@ -50,7 +50,16 @@ class SurroundWithArrayOfWithSpreadOperatorInFunctionFix(
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtExpression>? {
|
||||
val actualDiagnostic = Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION.cast(diagnostic)
|
||||
val actualDiagnostic = when (diagnostic.factory) {
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION ->
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION.cast(diagnostic)
|
||||
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR ->
|
||||
Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION_ERROR.cast(diagnostic)
|
||||
|
||||
else -> error("Non expected diagnostic: $diagnostic")
|
||||
}
|
||||
|
||||
val parameterType = actualDiagnostic.a
|
||||
|
||||
val wrapper = PRIMITIVE_TYPE_TO_ARRAY[KotlinBuiltIns.getPrimitiveArrayElementType(parameterType)] ?: ARRAY_OF_FUNCTION
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace with array call" "true"
|
||||
// COMPILER_ARGUMENTS: -XXLanguage:+ProhibitAssigningSingleElementsToVarargsInNamedForm
|
||||
// DISABLE-ERRORS
|
||||
|
||||
annotation class Some(vararg val strings: String)
|
||||
|
||||
@Some(strings = <caret>"value")
|
||||
class My
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace with array call" "true"
|
||||
// COMPILER_ARGUMENTS: -XXLanguage:+ProhibitAssigningSingleElementsToVarargsInNamedForm
|
||||
// DISABLE-ERRORS
|
||||
|
||||
annotation class Some(vararg val strings: String)
|
||||
|
||||
@Some(strings = ["value"])
|
||||
class My
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Surround with *arrayOf(...)" "true"
|
||||
// COMPILER_ARGUMENTS: -XXLanguage:+ProhibitAssigningSingleElementsToVarargsInNamedForm
|
||||
// DISABLE-ERRORS
|
||||
|
||||
fun anyFoo(vararg a: Any) {}
|
||||
|
||||
fun test() {
|
||||
anyFoo(a = in<caret>tArrayOf(1))
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Surround with *arrayOf(...)" "true"
|
||||
// COMPILER_ARGUMENTS: -XXLanguage:+ProhibitAssigningSingleElementsToVarargsInNamedForm
|
||||
// DISABLE-ERRORS
|
||||
|
||||
fun anyFoo(vararg a: Any) {}
|
||||
|
||||
fun test() {
|
||||
anyFoo(a = *arrayOf(intArrayOf(1)))
|
||||
}
|
||||
@@ -9547,6 +9547,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
runTest("idea/testData/quickfix/replaceWithArrayCallInAnnotation/literalWithValues.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("replaceForbiddenAssignmentWithArrayLiteral.kt")
|
||||
public void testReplaceForbiddenAssignmentWithArrayLiteral() throws Exception {
|
||||
runTest("idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceForbiddenAssignmentWithArrayLiteral.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("replaceSingleElementInNamedForm.kt")
|
||||
public void testReplaceSingleElementInNamedForm() throws Exception {
|
||||
runTest("idea/testData/quickfix/replaceWithArrayCallInAnnotation/replaceSingleElementInNamedForm.kt");
|
||||
@@ -10631,6 +10636,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
runTest("idea/testData/quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceForVarargOfAny.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("replaceForbiddenAssigningIntoArrayCall.kt")
|
||||
public void testReplaceForbiddenAssigningIntoArrayCall() throws Exception {
|
||||
runTest("idea/testData/quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceForbiddenAssigningIntoArrayCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("replaceToArrayOfPrimitiveTypes.kt")
|
||||
public void testReplaceToArrayOfPrimitiveTypes() throws Exception {
|
||||
runTest("idea/testData/quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceToArrayOfPrimitiveTypes.kt");
|
||||
|
||||
Reference in New Issue
Block a user