Suppress code analysis warnings for conditional methods

我的梦境 提交于 2019-12-11 03:01:38

问题


Running code analysis (Visual Studio 2015) on code that calls Conditional methods causes warnings for unused locals (CA1804) or unused parameters (CA1801). E.g. for:

using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        [Conditional ("NEVER_DEFINED")]
        static void Log(string message)
        {
            System.Console.WriteLine("Demo conditional message logging: " + message);
        }

        static void Main(string[] args)
        {
            string message = "Only log this when `NEVER_DEFINE` is #defined";
            Log(message);

            Method("other message");
        }

        static void Method(string messageToLog)
        {
            Log(messageToLog);
        }
    }
}

Code Analysis results in:

  • Warning CA1801

    Parameter 'args' of 'Program.Main(string[])' is never used. Remove the parameter or use it in the method body.

    ConsoleApplication1 …\Program.cs 14

  • Warning CA1804

    'Program.Main(string[])' declares a variable, 'message', of type 'string', which is never used or is only assigned to. Use this variable or remove it.

    ConsoleApplication1 …\Program.cs 15

  • Warning CA1801

    Parameter 'messageToLog' of 'Program.Method(string)' is never used. Remove the parameter or use it in the method body.

    ConsoleApplication1 …\Program.cs 22

I consider the warnings about message and messageTolog as false positives. Is there a way to have the code analysis treat the conditional methods as using the parameters?


回答1:


You can use the [SuppressMessageAttribute] in System.Diagnostics.CodeAnalysis. However you would have to apply it to each method that calls the Conditional-method (or apply it at broader scope); e.g.:

[System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Performance", "CA1804")]
static void Main(…)
{
    …
}

[System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Performance", "CA1801")]
static void Method(…)
{
    …
}



回答2:


Add the CODE_ANALYSIS conditional attribute to the called conditional method, e.g.:

using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        [Conditional ("NEVER_DEFINED")]
        [Conditional ("CODE_ANALYSIS")]
        static void Log(string message)
        {
            System.Console.WriteLine("Demo conditional message logging: " + message);
        }

        static void Main(string[] args)
        {
            string message = "Only log this when `NEVER_DEFINE` is #defined";
            Log(message);

            Method("other message");
        }

        static void Method(string messageToLog)
        {
            Log(messageToLog);
        }
    }
}

Now Log(string) is considered using its parameters and all parameters and local variables supplied to it are considered used as well.



来源:https://stackoverflow.com/questions/31944141/suppress-code-analysis-warnings-for-conditional-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!