Make diagnostic about redundant spread in @Foo(s = *[A]) more clear

This commit is contained in:
Mikhail Zarechenskiy
2018-07-06 15:44:58 +03:00
parent 513b27f65f
commit 6dc36055b8
10 changed files with 17 additions and 18 deletions
@@ -667,6 +667,7 @@ public interface Errors {
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);
DiagnosticFactory0<KtExpression> REDUNDANT_SPREAD_OPERATOR_IN_NAMED_FORM_IN_ANNOTATION = DiagnosticFactory0.create(WARNING);
// Call resolution
@@ -783,6 +783,7 @@ public class DefaultErrorMessages {
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(REDUNDANT_SPREAD_OPERATOR_IN_NAMED_FORM_IN_ANNOTATION, "Redundant spread (*) operator");
MAP.put(CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS, "Cannot create an instance of an abstract class");
@@ -66,19 +66,15 @@ 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()) {
// We want to make calls @Foo(value = [A]) and @Foo(value = *[A]) equivalent
report(onlyWarning = true)
context.trace.report(Errors.REDUNDANT_SPREAD_OPERATOR_IN_NAMED_FORM_IN_ANNOTATION.on(argumentExpression))
}
} else {
report()
reportMigrationDiagnostic(migrationDiagnosticsForAnnotation, context) { diagnostic ->
context.trace.report(diagnostic.on(argumentExpression))
}
}
}
@@ -100,11 +96,10 @@ class AssigningNamedArgumentToVarargChecker : CallChecker {
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)) {
if (context.languageVersionSettings.supportsFeature(ProhibitAssigningSingleElementsToVarargsInNamedForm)) {
report(error)
} else {
report(warning)