Do not report diagnostic about annotation with receiver target twice

This commit is contained in:
Mikhail Zarechenskiy
2017-09-11 19:25:53 +03:00
parent 2048a74302
commit e16d16fdb7
9 changed files with 14 additions and 40 deletions
@@ -244,7 +244,6 @@ public interface Errors {
DiagnosticFactory1<PsiElement, String> INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> INAPPLICABLE_TARGET_PROPERTY_HAS_NO_DELEGATE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INAPPLICABLE_TARGET_PROPERTY_HAS_NO_BACKING_FIELD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INAPPLICABLE_RECEIVER_TARGET = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INAPPLICABLE_PARAM_TARGET = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> REDUNDANT_ANNOTATION_TARGET = DiagnosticFactory1.create(WARNING);
DiagnosticFactory0<KtAnnotationUseSiteTarget> INAPPLICABLE_FILE_TARGET = DiagnosticFactory0.create(ERROR);
@@ -158,7 +158,6 @@ public class DefaultErrorMessages {
MAP.put(INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE, "''@{0}:'' annotations could be applied only to mutable properties", TO_STRING);
MAP.put(INAPPLICABLE_TARGET_PROPERTY_HAS_NO_DELEGATE, "'@delegate:' annotations could be applied only to delegated properties");
MAP.put(INAPPLICABLE_TARGET_PROPERTY_HAS_NO_BACKING_FIELD, "'@field:' annotations could be applied only to properties with backing fields");
MAP.put(INAPPLICABLE_RECEIVER_TARGET, "'@receiver:' annotations can only be applied to the receiver type of extension function or extension property declarations");
MAP.put(INAPPLICABLE_PARAM_TARGET, "'@param:' annotations could be applied only to primary constructor parameters");
MAP.put(REDUNDANT_ANNOTATION_TARGET, "Redundant annotation target ''{0}''", STRING);
MAP.put(INAPPLICABLE_FILE_TARGET, "'@file:' annotations can only be applied before package declaration");
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
@@ -37,32 +36,6 @@ object AnnotationUseSiteTargetChecker {
trace.checkDeclaration(parameter, parameterDescriptor)
}
}
if (descriptor is CallableDescriptor) trace.checkReceiverAnnotations(descriptor)
}
private fun BindingTrace.checkReceiverAnnotations(descriptor: CallableDescriptor) {
val extensionReceiver = descriptor.extensionReceiverParameter ?: return
for (annotationWithTarget in extensionReceiver.type.annotations.getUseSiteTargetedAnnotations()) {
val target = annotationWithTarget.target ?: continue
fun annotationEntry() = DescriptorToSourceUtils.getSourceFromAnnotation(annotationWithTarget.annotation)
when (target) {
AnnotationUseSiteTarget.RECEIVER -> {}
AnnotationUseSiteTarget.FIELD,
AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD,
AnnotationUseSiteTarget.PROPERTY,
AnnotationUseSiteTarget.PROPERTY_GETTER,
AnnotationUseSiteTarget.PROPERTY_SETTER,
AnnotationUseSiteTarget.SETTER_PARAMETER -> {
annotationEntry()?.let { report(INAPPLICABLE_TARGET_ON_PROPERTY.on(it, target.renderName)) }
}
AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER -> annotationEntry()?.let { report(INAPPLICABLE_PARAM_TARGET.on(it)) }
AnnotationUseSiteTarget.FILE -> {
annotationEntry()?.useSiteTarget?.let { reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(it)) }
}
}
}
}
private fun BindingTrace.checkDeclaration(annotated: KtAnnotated, descriptor: DeclarationDescriptor) {
@@ -92,7 +65,7 @@ object AnnotationUseSiteTargetChecker {
}
AnnotationUseSiteTarget.SETTER_PARAMETER -> checkIfMutableProperty(annotated, annotation)
AnnotationUseSiteTarget.FILE -> reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(useSiteTarget))
AnnotationUseSiteTarget.RECEIVER -> report(INAPPLICABLE_RECEIVER_TARGET.on(annotation))
AnnotationUseSiteTarget.RECEIVER -> {}
}
}
}