Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code. The syntax for these suppressions is similar to that for Visual Studio Code Analysis, or FxCop. For more information about Code Analysis suppressions, see the following article: In Source Suppressions Overview.

StyleCop rule suppressions are registered in code using the SuppressMessage attribute. The SuppressMessage attribute is a conditional attribute, which is included in the IL metadata of your managed code assembly only if the CODE_ANALYSIS compilation symbol is defined at compile time.

We recommend using in-source suppressions on debug builds, in order to eliminate the possibility of mistakenly shipping the in-source suppression metadata and compromising execution or performance because of the metadata bloat.

The SuppressMessage attribute has the following format:

[SuppressMessage("Rule Category, "Rule Id", "Justification")]

Where:

The SuppressMessage attribute also takes the following optional parameters. These parameters are completely ignored by StyleCop and do not need to be filled in for StyleCop suppressions.

SuppressMessage Usage

StyleCop violations are suppressed at the level to which an instance of the SuppressMessage attribute is applied. The purpose of this is to tightly couple the suppression information to the code where the violation occurs.

For example, a StyleCop SuppressMessage attribute placed on a class will suppress the rule for all contents of the class. The same attribute placed on a method will only suppress the rule within the method.

Global Suppressions

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

Examples:

The following code suppresses the ElementsMustBeDocumented rule for the given class and all its contents:

[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented")]

public class MyUndocumentedClass

{

    public void MyUndocumentedMethod

    {

    }

}

Beginning with StyleCop 4.4.0, it is also possible to suppress all of the rules within a rule namespace, using a single suppression attribute. This is indicated by replacing the rule CheckID and rule name with a single asterisk. The following code example suppresses all of StyleCop's default documentation rules within the inner class. In this case, StyleCop would still flag a violation indicating that the outer class is missing documentation, but it would ignore all documentation rules for the inner class and its contents.

public class OuterClass

{

    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "*")]

    public class InnerClass

    {

        public void MyUndocumentedMethod

        {

        }

    }

}