FP: S3993 requires abstract attributes to be decorated with [AttributeUsage]

Rule S3993 tells us that we should decorate our attributes with [AttributeUsage], so that we define where the usage of our attributes is allowed, and can be enforced by the compiler.

However, for abstract attributes, enforcing this makes no sense to me, as these attributes can not be used (directly). So, this example should be compliant:

public abstract class AbstractAttribute : Attribute // Compliant

And if my observation is wrong, because there is a good reason also to enforce S3993 on abstract attributes, please update the documentation on why this would make sense on abstract attributes too.

Hi @Corniel,

I agree with you: there is likely no point in giving an [AttributeUsage] to an abstract attribute.
On the one hand, the attribute usage would get inherited (the usage of AttributeUsage is [AttributeUsage(AttributeTargets.Class, Inherited=true)]).
However, from a check on sourcegraph.com, it seems that the vast majority of abstract attributes don’t specify their usage.
Moreover, CA1018 has the same exception (see here), and it doesn’t trigger when the attribute is abstract.
For these reasons, I am going to follow your suggestion and proceed to integrate the related PR in sonar-dotnet.

Best regards,

1 Like

Thanks. Note that S3993 even requires you define the usage on inherited attributes for clarity reasons, so if you define in on an base attribute, according to S3993 you still have define it on those inherited attributes.

1 Like

Indeed. That would make the attribute usage on the abstract class redundant in the best case (when the attribute usage of the derived class matches), and misleading in the worst (when it doesn’t).

The PR is merged and is going to be included in v. 9.23 of the C# Analyzer.

Thanks a lot for your contribution, it’s really appreciated!

1 Like