D J
D J

Reputation: 7028

Disable all stylecop warnings for a specific C# class

I am working in an application and developing few classes for demo purpose. I know that these classes will be removed in future.

Is it possible to ignore all the stylecop warnings for those classes as I dont want to spent the time on those warnings?

I searched but found that I can only ignore via settings in stylecop( this will effect other classes too) or to some specific rule ( I just want to ignore all warnings).

Upvotes: 9

Views: 15689

Answers (5)

Jon G
Jon G

Reputation: 4164

Thanks to Bartłomiej Mucha for the answer I've just used. As I discovered, the "*" works well for the specific rule, but you do have to add suppressions for each category. Here's the complete set - if you copy these into the top of a class, you should find that all StyleCop errors are suppressed:

[SuppressMessage("StyleCop.CSharp.NamingRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.LayoutRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.OrderingRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.SpacingRules", "*", Justification = "Reviewed. Suppression is OK here.")]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "*", Justification = "Reviewed. Suppression is OK here.")]
internal class MyClass 
{
    // ...
}

Upvotes: 13

Nicole Calinoiu
Nicole Calinoiu

Reputation: 20992

If you want to prevent StyleCop from running over a file, you can mark it excluded in your .csproj file using the ExcludeFromStyleCop attribute mentioned at http://stylecop.codeplex.com/wikipage?title=Using%20StyleCop%20on%20Legacy%20Projects&referringTitle=Documentation.

Upvotes: 1

Bartłomiej Mucha
Bartłomiej Mucha

Reputation: 2782

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

Upvotes: 15

Jon
Jon

Reputation: 437386

You can trick StyleCop into not processing a file at all by adding this header at the top:

//------------------------------------------------------------------------------
// <auto-generated>
// Well, not really. This is just a trick to get StyleCop off my back.
// </auto-generated>
//------------------------------------------------------------------------------

Upvotes: 25

user1968030
user1968030

Reputation:

You can suppress rules by adding attributes to blocks of code. Here's a simple example on a class from the blog post linked below, but you can do it on various members individually:

[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented")]
public class MyUndocumentedClass
{
    public void MyUndocumentedMethod {}
}

Upvotes: 0

Related Questions