Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable StyleCop for specific lines

Tags:

.net

stylecop

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?

like image 793
stiank81 Avatar asked Nov 18 '09 10:11

stiank81


People also ask

How do I turn off StyleCop rules?

You can see this in the warnings section when you run stylecop in your project. Find with key word SA1005 and you should see that in result section. Just uncheck.

How do I turn off StyleCop analyzers?

right-click on a project in Project Explorer. select "StyleCop Settings" on the "Rules" tab of the dialog that opens, uncheck the "C#" root of the Enabled rules tree.


3 Answers

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.

like image 96
FinnNk Avatar answered Oct 20 '22 11:10

FinnNk


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
like image 21
Adam Butler Avatar answered Oct 20 '22 09:10

Adam Butler


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.

like image 6
satnhak Avatar answered Oct 20 '22 09:10

satnhak