How to suppress a StyleCop warning?

Solution 1:

Here's what you need:

[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")]

Solution 2:

An example of inline suppression would be similar to this - examine the namespaces in the code compared to the suppression

namespace Soapi
{
        ///<summary>
        ///</summary>
        ///<param name = "message"></param>
        ///<param name = "statusCode"></param>
        ///<param name = "innerException"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]
        public ApiException(string message, ErrorCode statusCode, Exception innerException)
            : base(String.Format("{0}\r\nStatusCode:{1}", message, statusCode), innerException)
        {
            this.statusCode = statusCode;
        }

A global supression file is a file in the root of your project named GlobalSuppressions.cs and might look like this:

// This file is used by Code Analysis to maintain SuppressMessage 
// attributes that are applied to this project. 
// Project-level suppressions either have no target or are given 
// a specific target and scoped to a namespace, type, member, etc. 
//
// To add a suppression to this file, right-click the message in the 
// Error List, point to "Suppress Message(s)", and click 
// "In Project Suppression File". 
// You do not need to add suppressions to this file manually. 

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)", Scope = "member", Target = "Soapi.ApiException.#.ctor(System.String,Soapi.ErrorCode,System.String,System.Exception)")]

And you can generate this code automatically by right-clicking on the warning.

Solution 3:

Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code.

Rule Suppressions http://stylecop.soyuz5.com/Suppressions.html

but it says -

Global Suppressions

StyleCop does not support the notion of global suppressions or file-level suppressions. Suppressions must be placed on a code element.

Solution 4:

If you've installed StyleCop, you can right-click your project and there will be a StyleCop option. Click this and you'll see you can prevent certain rules from even running against your project. Moreover, you can create a separate rules file to share between different projects. This means you can configure the rules once the way you want them and then share that configuration between all your projects.

For individual overrides, SuppressMessage is the way to go.