How to suppress a StyleCop warning?

Viewed 59676

I'm using StyleCop and want to suppress some warning which does not suit my style. I prefer to have solution for

1) in-line code suppressing
2) global setting suppressing

I've searched the internet but still not sure how to do the suppressing.

For method 1), They said to add the lines:

[assembly: SuppressMessage("Microsoft.Design", "SA1202:All private methods must be placed after all public methods", Scope = "namespace", Target = "Consus.Client.ClientVaultModule.Services.OnlineDetection")]

But they do not say where and which namespace to be used.

For method 2), they said to use GlobalSuppress file but it seems not easy to search for a how-to do it at the moment.

Please help.

[Edited] In my case, I have the warning about SA1202: All private methods must be placed after all public methods which is bothering since I group my related codes into regions. I want to suppress those warning for just some certain methods.

12 Answers
  1. Go to Solution Explorer
  2. Go to your project
  3. Expand references
  4. Expand Analyzers
  5. Expand StyleCop.Analyzers
  6. Right click on a particular rule which you want to disable at a global (project) level
  7. Set Rule Set severity -> Select None

The README.md for the StyleCop.Analyzers NuGet package used by Visual Studio 2015+ contains a link to the documentation for the rules. The documentation for each rule contains a "How to suppress violations" section. For the SA1202 rule, the options are:

[SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]

and

#pragma warning disable SA1202 // ElementsMustBeOrderedByAccess
#pragma warning restore SA1202 // ElementsMustBeOrderedByAccess
Related