Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Suppressing Code Analysis Warning

, 10 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Suppressing Code Analysis Warning

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

A Bit of History

Yesterday, I was working at a few code modules wherein I was supposed to fix some FxCop warnings. While doing so, I had a few blockages while fixing few warnings, wherein those warnings fix changes that lead to minor and sometimes major design changes. Of course, it's a standard guideline that when you’re doing static code analysis (FxCop in this case), do not intent to change the design. I believe 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. This is very easy, but at my work, everything 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 the below image:

Let's 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 supposed to change rule sets, then it's pretty tough. Thanks to SuppressMessage attribute provided by FCL.

So I added the 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 the below image:

The second argument is CheckID which again you can get from FxCop. Third onwards 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 make the 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 the below image:

After making 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 something 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.

That's all for now, hope you liked it.

Thanks. :)

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


Filed under: c#, CodeProject, dotnet Tagged: .NET, blog, c#, codeproject, dotnet, tips

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

zenwalker1985
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.
 
Loves Trekking/Hiking, animals and nature.
 
A FOSS/Linux maniac by default Wink | ;)
 
An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/
Follow on   Twitter

Comments and Discussions

 
QuestionPVS-Studio PinmemberKarpov Andrey14-Mar-12 21:03 
QuestionJustifications ! PinmemberKabwla.Phone14-Nov-11 21:48 
AnswerRe: Justifications ! Pinmemberzenwalker198515-Nov-11 1:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 10 Nov 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid