2

I have a class which starts off something like this:

namespace Tools.Builders
{
    internal abstract class Builder
    {
        [SuppressMessage("Microsoft.Maintainability", "CA1502")]
        private static readonly Dictionary<string, Func<ILogger, Builder>> _builders =
            new Dictionary<string, Func<ILogger, Builder>>
        {
            { "1", (x) => {return new BuilderType1(x);} },
            { "2", (x) => {return new BuilderType2(x);} },
            { "3", (x) => {return new BuilderType3(x);} },
            { "4", (x) => {return new BuilderType4(x);} },
            { "5", (x) => {return new BuilderType5(x);} },
            { "6", (x) => {return new BuilderType6(x);} },
            { "7", (x) => {return new BuilderType7(x);} },
            { "8", (x) => {return new BuilderType8(x);} },
            { "9", (x) => {return new BuilderType9(x);} },
        };
        protected ILogger _logger;
        protected Builder(ILogger logger)
        {
            _logger = logger;
        }
        //...

This causes a CA1502 warning of the form "Builder.Builder() has a cyclomatic complexity of..." (which is a known problem with this sort of initialiser). However my problem is I can't suppress the warning. I've tried putting the SuppressMessageAttribute in all sorts of different places in the code, but it just gets ignored. Any suggestions anyone?

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
Dave
  • 3,429
  • 2
  • 26
  • 29
  • [Docs](https://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.suppressmessageattribute(v=vs.110).aspx): _"The preprocessor symbol "CODE_ANALYSIS" must be defined for this attribute to be effective. The absence of the preprocessor symbol results in the attribute not being applied."_ – CodeCaster Sep 02 '17 at 16:23
  • Well, I tried it, but it didn't make any difference. Thanks anyway. – Dave Sep 02 '17 at 20:12
  • See [Pragma not working for warning CA1502](https://social.msdn.microsoft.com/Forums/en-US/872d9c97-02b7-4409-9df7-6202ea0820dc/pragma-not-working-for-warning-ca1502?forum=vstscode) –  Sep 04 '17 at 08:24
  • I'm not sure what the point is here. Someone suggested I try #pragma warning(disable: 1502), but that comment seems to have disappeared. As buffjape's link points out, that would be for compiler warnings not CA warnings so not much help. Meanwhile I'm not getting any further with the actual problem. – Dave Sep 04 '17 at 09:49

2 Answers2

3

Applying the following attribute to the method in question worked for me in VS 2017

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]

3

I was able to suppress this message by using an assembly-level attribute that specifies the constructor as the target:

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Microsoft.Maintainability",
                           "CA1502:AvoidExcessiveComplexity",
                           Scope = "member",
                           Target = "Tools.Builders.Builder.#.cctor()")]

This attribute can be placed in any code file in the assembly.

For future reference, I generated this attribute by right-clicking the CA warning in the Error List window (or the Code Analysis window in VS2013 and earlier) and selecting Suppress -> In Suppression File.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
  • That's a neat trick. Copy the attribute out of GlobalSuppressions.cs, which can then be removed - much tidier to have it in the source to which it applies. This fixed my problem, thanks. (BTW you meant the Code Analysis window not the Error List window) – Dave Nov 02 '17 at 10:03
  • @Dave Starting with VS2015 they combined those windows - I'll edit to note the difference. – BJ Myers Nov 02 '17 at 16:58