All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S3993.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

When defining custom attributes, AttributeUsageAttribute must be used to indicate where the attribute can be applied. This will:

  • indicate how the attribute can be used
  • prevent it from being used at invalid locations

How to fix it

Code examples

Noncompliant code example

public sealed class MyAttribute : Attribute // Noncompliant - AttributeUsage is missing
{
    private string text;

    public MyAttribute(string text)
    {
        this.text = text;
    }

    public string Text => text;
}

Compliant solution

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
public sealed class MyAttribute : Attribute
{
    private string text;

    public MyAttribute(string text)
    {
        this.text = text;
    }

    public string Text => text;
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy