S109 - magic numbers on attributes

We are using a lot of numbers in data transfer object attributes, so in each file we have to suppress this warning.
Currently C# does not support using variables in attributes, so there’s no way to avoid magic numbers from being used in attributes:

        [Properties(ModelMap: "key", Order: 10)]
        public int? PrimaryKey { get; set; }

hi @donatasj87

in this case, it looks like it would be easier to disable the rule for your project

I don’t want to disable it for all project, because it is quite useful rule in other parts of the project, so I end up disabling it on each file. Would be nice if sonar could detect it and ignore automatically.

No, but it can still use constants

    public class AttributeUsage
        private const int MAGIC = 10;
        public int? Foo { get; set; }


    class MyCustomAttribute : Attribute
        public int ParamValue { get; set; }
        public MyCustomAttribute(int paramValue)
            this.ParamValue = paramValue;

Correct. It is useful for static and reusable values, but in some cases each properties have different values, like ordering for example, so in this case creating constant for each one would be a waste of keystrokes.

hi @donatasj87

We’ve had an internal talk. We agree that in your case this rule is creating a lot of noise. However, for now we don’t plan to change it to detect all exceptions that it would have to handle.

  • the rule is not part of SonarWay (we know it can generate quite some noise, so by default it’s turned off)
  • it would require quite some effort to tweak the rule to make it less noisy, and we don’t plan to invest that effort any time soon

Thank you for the feedback! We’ll keep it in mind for the future.

is there a way to disable this rule in some files?

@Daniel-Gabriel Please have a look at section “Ignore Issues” in the documentation.

1 Like