How to Suppress a Stylecop Warning

Suppressing the StyleCop Warnings at the global level

StyleCop doesn't support the notion of global suppression of a rule. It requires the SuppressMessage attribute be placed on the given code element.

From the Source Analysis Blog (Source)

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

One option though is to simply turn off the rules that you aren't interested in. This is the preferred method of global suppression.

Disable all stylecop warnings for a specific C# class

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
{
}
}
}

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

How to suppress a StyleCop warning of entire namespace files

The SuppressMessage directive will only process the current file (see the comments in this stylecop issue).

You could possibly achieve what you want via file lists, although this will work best if there are only a few files you want to apply this exclusion to. If you want this applied to all files in a particular project, then the easiest way may be to simply modify the settings via the settings dialog as already mentioned.

Alternatively, you could put all the namespace related files into a separate folder, and have a custom settings file in that folder to exclude these rules from those particular files.

StyleCop : Warning not suppressed

I use Settings.StyleCop file. You can configure it easily with Stylecop plugin for VS.

Right click on the specific project -> StylecopSettings

Sample Image

You can choose then what rules you want to skip in the warnings tab

Sample Image

That generates a Settings.StyleCop file by Project.

How to configure StyleCop to suppress warnings on generated code?

StyleCop: How To Ignore Generated Code

Edit: Here is the header I use in generated grammars for ANTLR. This is actually the body of a StringTemplate template, so the two \> entries are actually just escaped > marks. Aside from the <auto-generated> tag and the [GeneratedCode] attribute, we still had to disable some warnings which appeared during code analysis.

//------------------------------------------------------------------------------
// \<auto-generated>
// This code was generated by a tool.
// ANTLR Version: ANTLRVersion
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// \</auto-generated>
//------------------------------------------------------------------------------

// $ANTLR <ANTLRVersion> <fileName>

// The variable 'variable' is assigned but its value is never used.
#pragma warning disable 219
// Unreachable code detected.
#pragma warning disable 162
// Missing XML comment for publicly visible type or member 'Type_or_Member'
#pragma warning disable 1591
// CLS compliance checking will not be performed on 'type' because it is not visible from outside this assembly.
#pragma warning disable 3019
// 'type' does not need a CLSCompliant attribute because the assembly does not have a CLSCompliant attribute.
#pragma warning disable 3021

[System.CodeDom.Compiler.GeneratedCode("ANTLR", "<ANTLRVersion>")]
[System.CLSCompliant(false)]
public class ...


Related Topics



Leave a reply



Submit