Suppressing Code Analysis Warning

Dear Reader,

This post is regarding suppressing few warnings while doing static code analysis for your project via tools such as FxCop.

A Bit History:

Yesterday i was working on few code modules where in i was supposed to fix some FxCop warnings. While doing so, i had few blockages while fixing few warnings, where in those warnings fix changes lead to minor and some times major design changes. Of course its a standard guidelines that when you’re doing static code analysis (FxCop in this case) do not intent to change the design. I belive that this analysis phase is part of Refactoring.

But i wanted a way to escape from those warnings, of course one way is to customize the rule sets of FxCop tool. Which is very easy, but at my work, every thing is pre-configured rules for this tool and i am not supposed to change it no matter what.

So i started to look for some other way to solve this problem. This post is just to share what i have learned on the same. Hope you will like it :)

Let me start off by presenting a small code for which i am doing code analysis via FxCop.

static Liststr = new List();
public static void Main()
{
for (int i = 0; i < 5; i++)
{
str.Add(new Program());
new Program();
}
Console.ReadLine();
}

So logically there is no problem with the above code and yes you sure will get few warnings in fxCop as shown in below image:

Lets assume that i am supposed to fix the above shown warnings which are pretty commonly occurring ones for all but assume that those warnings may lead to design change or you’re not interested to fix it. As said earlier, if you had to remove those warnings in your code but not suppose to change rule sets then its a pretty tough. Thanks to SuppressMessage attribute provided by FCL.

So i added below attribute code to the Main method as shown:

[SuppressMessage(“Microsoft.Usage”,”CA1806″, Justification=”I do not care to fix”, Scope=”Just for this method”)]
public static void Main()
{
for (int i = 0; i < 5; i++)
{
str.Add(new Program());
new Program();
}
Console.ReadLine();
}

As you can see, SuppressMessage() attribute takes a couple of arguments. The first argument is the category of the warning which in this case you can get from the FxCop tool as shown in below image:

The second argument is CheckID which again you can get from FxCop. Third on wards arguments are named parameters, where are optional. In the above code, i have written some justification statement for Justification parameter argument followed by the scope of this suppress attribute. You can also place this attribute for class, fields, properties, methods, etc. which is quite helpful actually.

Now once i do above changes, FxCop will not show any changes in its output actually. This is because, we need to set Conditional Compilation Symbol to CODE_ANALYSIS in project properties as shown in below image:

After doing the above changes, compile the code and start over FxCop analysis. Now you shall notice that those warnings which you knocked off with SuppressMessage attribute will not be reported by FxCop tool. Please note that you need to do this only for Debug build and not for Release build.

Also there is some thing i also learned in this tool i.e FxCop that you can set the target framework for the specifics extra rules set check to just make sure there is no non targeted framework API gets included.

What i mean is when a user selects .NET Framework 3.0 as their project’s target framework, FxCop will fire on any usage of members and types that were introduced in .NET Framework 2.0 SP1 and SP2, etc. This is because on a fresh install of .NET Framework 3.0  in an OS, none of these service packs or frameworks are installed

Thats all for now, hope you liked it.

Thanks :)

P.S: Your comments/votes are much appreciated :)

One comment

  1. Andrey Karpov (@Code_Analysis) · November 1, 2011

    Hello.
    I want to suggest try our PVS-Studio Static Code Analyzer for C/C++/C++11.
    I hope you enjoy it. I am willing to help and answer questions.
    Examples of use PVS-Studio: http://www.viva64.com/en/a/0077/
    Download fully functional trial: http://www.viva64.com/en/pvs-studio-download/

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s