DCSIMG
Suppress Code Analysis Warnings In Code - Maor's Blog

Suppress Code Analysis Warnings In Code

Code analysis provides a way of making sure our assemblies don’t violate the programming and design rules set forth in the Microsoft .NET Framework Design Guidelines (including issues related to Globalization, Security, Performance, Portability, and many more).  We can run code analysis from the IDE and from MSBuild. To do it, we first have to turn on Code Analysis from within the IDE. To do it we have to open the project properties, and in the Project designer, we should select the Code Analysis tab and check the Enable Code Analysis on Build checkbox.

ca3

By default, violations will only appear as warnings in the Error List window. Alternatively, you can flag violations as errors by placing a check in the Treat Warning as Error column for a particular rule.

Now, when code analysis is enabled we can build our project. For example, I wrote Foo method that never used. After I built the project I got the following warning:

ca1

I got CA1811 warning, which means that the code analysis engine found that this method never used. I can use System.Diagnostics.CodeAnalysis.SuppressMessage attribute to suppress this warning:

[System.Diagnostics.CodeAnalysis.SuppressMessage(
  "Microsoft.Performance",
  "CA1811:AvoidUncalledPrivateCode")]
private static void Foo(int num1, int num2)
{
    int[] numbers = new int[5] { 1, 2, 3, 4, 5 };
    if (numbers[num1] == num2)
    {
        numbers[num1] = 1;
    }
}

To use this attribute we should add System.Diagnostics.CodeAnalysis namespace. The SuppressMessage class got 2 parameters:

  1. The category identifying the classification of the attribute. (Microsoft.Performance in our case)
  2. The identifier of the static analysis tool rule to be suppressed. (CA1811:AvoidUncalledPrivateCode in our case).

After adding this attribute, the warning disappeared…

ca2

 

Share this post :
Published 23 June 2008 04:40 PM by Maor David-Pur

Comments

# אוהד אסטוןŸ said on 23 June, 2008 05:48 PM

hmmm... is there any way to do that on files (Suppress warnings) :-P

# 2 Static » Blog Archive » Suppress Code Analysis Warnings In Code said on 23 June, 2008 07:38 PM

Pingback from  2 Static  » Blog Archive   » Suppress Code Analysis Warnings In Code

# Team System News said on 27 June, 2008 12:22 AM

Brian Harry on Rally connects to TFS for Agile project management and Automating the creation of Team...

# stibleHiene said on 07 July, 2008 04:57 PM

tests time mashine

# Paul Verrone said on 10 July, 2008 07:47 PM

There is an easier way to do this:   In the warning list in the UI, right click and select Suppress Message(s).  Then you can choose to suppress the message in the code file (which does what you listed above) or "In Project Suppression File" (globalsuppressions.vb/cs).  I ask my developers to use the global suppressions file so that I can review and see all the suppressions in one place.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:

Search

Go

This Blog

News

    RSS

     

    Connect with Me

    Maor's Facebook profile  Follow Maor on Twitter  Maor's profile on Linkedin  Maor in FriendFeed 
           

Syndication