38

We're using StyleCop in our C# projects. In some cases we'd like to avoid the rules though. I know you can add // <auto-generated /> in the beginning of the file to make StyleCop ignore it. However, I don't want to ignore the rules for the whole file - only a block of code within it.

Can I disable StyleCop for specific lines somehow?

stiank81
  • 25,418
  • 43
  • 131
  • 202

6 Answers6

37

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

There's a quick overview at an MSDN blog post and a fuller description of the attributes on MSDN.

FinnNk
  • 3,249
  • 23
  • 25
  • Thanks! This should work, though I was hoping for a simpler approach. Was hoping for an method where I can simply say "ignore StyleCop for these lines" - instead of having to find the correct text for style names etc. Awaiting to see if this is possible. – stiank81 Nov 18 '09 at 11:51
  • 2
    AFAIK it's not possible - the ability to suppress messages is itself a fairly recent addition. – FinnNk Nov 18 '09 at 12:12
  • Thanks. You're probably right then, but I'm gonna leave the question out there for a little longer.. – stiank81 Nov 18 '09 at 12:19
  • Well - no new answers for a week, so assume my wish isn't achievable yet then.. Thanks for your answer! – stiank81 Nov 25 '09 at 08:22
  • 5
    Note that you'll need a Justification="" parameter for that attribute - or you'll get the StyleCop warning SA1404: "A Code Analysis suppression must contain a non-empty justification describing the reason for the suppression." – g t Apr 12 '11 at 13:40
  • 3
    To have StyleCop ignore "these lines" wrap the lines in a region and have the opening region contain the words "generated code" (like "This is not generated code but rather code that StyleCop should ignore." – Dracorat Feb 10 '12 at 22:32
  • Can I add these somehow to a region? – TK-421 Apr 07 '21 at 12:25
13

An old question I know but in looking for an answer I found that in stylecop 4.4 you can now put something like this - or one of these lines on a method:

[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "*", Justification = "Risky to change manually")]
[SuppressMessage("Microsoft.StyleCop.CSharp.SpacingRules", "*", Justification = "Risky to change manually")]
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "*", Justification = "Risky to change manually")]
[SuppressMessage("Microsoft.StyleCop.CSharp.LayoutRules", "*", Justification = "Risky to change manually")]
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "*", Justification = "Risky to change manually")]
[SuppressMessage("Microsoft.StyleCop.CSharp.ReadabilityRules‌​", "*", Justification = "Risky to change manually")]
[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "*", Justification = "Risky to change manually")]
  • Note: I might be missing one or two of the rule categories
Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34
Adam Butler
  • 3,023
  • 5
  • 35
  • 40
  • 1
    This did the trick for me. FYI, I editted the last two messages since they had "Microsoft.StyleCop.CSharp" duplicated in the namespace – Jacob Adams Nov 11 '11 at 18:14
  • 1
    I recently had to add this for a library class and found that I needed [SuppressMessage("Microsoft.StyleCop.CSharp.ReadabilityRules", "*", Justification = "This is library code")] as well – Evan M Nov 16 '11 at 16:39
  • 1
    Ordering rules is another one you may need to add: `[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "*", Justification = "Risky to change manually")] ` – Electric Sheep May 23 '16 at 10:10
6

This guy seems to have a good general ignore hack; he suggests putting this at the top of the file - tested and working with R#

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// </auto-generated>
//------------------------------------------------------------------------------

Handy for when you are just churning out a load of boilerplate to adhere to a mainly unimplemented interface, for example.

satnhak
  • 9,407
  • 5
  • 63
  • 81
0

You can put the piece of code between a region name Generated Code and it will be ommited.

sGambolati
  • 783
  • 8
  • 25
0

Decorate your class or method with the following StyleCop attribute:

[GeneratedCode("Tool Goes Here", "Message Goes Here")]
jezzipin
  • 4,110
  • 14
  • 50
  • 94
0

You can disable a block of code like so:

// some other code before and after

#pragma warning disable SA1008 // Opening parenthesis must not be preceded by a space
     (_, value) => (key, value)))
#pragma warning restore SA1008
Appetere
  • 6,003
  • 7
  • 35
  • 46