Suppressing FxCop Messages

August 23rd, 2006 Adrian Banks 8145 Views

Using the latest version (1.35) of FxCop, it is possible to exclude generated warning messages in the source code instead of having to exclude them in the FxCop project file. This is accomplished using the System.Diagnostics.CodeAnalysis.SuppressMessageAttribute class.

To exclude a message, simply mark up the method with the SuppressMessage attribute, declaring both the rule category and the specific rule to exclude:

[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
public string ConvertToString(object obj)
{
    return obj.ToString();
}

The above example will exclude the "Validate Arguments Of Public Methods" rule from the Design category for the ConvertToString method (although in this contrived example it is probably a bad idea to do so as passing in a null will clearly cause problems).

One extra "tweak" that can be utilised in this scenario is the Justification property. Altering the above code to:

[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification="I do have a valid reason, honest!")]
public string ConvertToString(object obj)
{
    return obj.ToString();
}

will allow the person excluding a message to provide a reason for doing so in the code, alongside the exclusion. FxCop (v1.35) currently doesn't display this in its output, but will do in the next release (source). It does however output the justification to a generated report if the options are set to output exclusions to the report.

Excluding FxCop messages in the source code has advantages over excluding them in the FxCop project as it demonstrates that the message has been specifically excluded for that particular case, but also will withstand class and namespace changes. It also makes switching from the standalone FxCop to Visual Studio's code analysis an easier process.

In order to allow the SuppressAttribute to work, a CODE_ANALYSIS conditional compilation symbol must be defined for the project. Without this, FxCop will ignore the suppressed attribute and will still generate a warning.

More details can be found on the FxCop blog.

Entry Filed Under: C#, .Net


Subscribe to this feed Bookmark this post on del.icio.us Digg this post on digg.com Submit this post to DotNetKicks Kick this postShare this post

2 Comments Add your own

  • 1. abi  |  January 15th, 2007 at 4:41

    Thanks, your comment at the end about the CODE_ANALYSIS flag really helped a lot :)

  • 2. Mansoor Mehmood  |  March 18th, 2010 at 13:10

    How can I exclude all warning from a specific class or Type
    OR
    Is it possible to exclude all types in a specific namespace

    Actually I have come code in project which is auto generated. how Can I suppress warnings on this?

    Mansoor Mehmood

Leave a Comment

Required

Required, hidden

Please enter the following word: magic word

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Popular Posts



Calendar

September 2010
M T W T F S S
« Jun    
 12345
6789101112
13141516171819
20212223242526
27282930  

Most Recent Posts

Related Posts

RSS Feeds Feeds

Visitors

Visitor Map

Total Hits: 92663
Hits Today: 78