Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress a StyleCop warning?

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.

like image 737
Nam G VU Avatar asked Jul 20 '10 06:07

Nam G VU


People also ask

How do I turn off StyleCop?

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.

How do I open StyleCop settings editor?

The Settings. StyleCop file can be opened from the Explorer and edited through a GUI. If that doesn't work, try dragging the Settings file onto an application called StyleCopSettingsEditor.exe which is installed in your StyleCop Program Files folder.

What is StyleCop JSON?

stylecop.jsonCode analysis rule sets are the standard way to configure most diagnostic analyzers within Visual Studio. Information about creating and customizing these files can be found in the Using Rule Sets to Group Code Analysis Rules documentation on docs.microsoft.com.


2 Answers

Here's what you need:

[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess")] 
like image 143
Jason Allor Avatar answered Sep 24 '22 13:09

Jason Allor


An example of inline suppression would be similar to this - examine the namespaces in the code compared to the suppression

namespace Soapi {         ///<summary>         ///</summary>         ///<param name = "message"></param>         ///<param name = "statusCode"></param>         ///<param name = "innerException"></param>         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object)")]         public ApiException(string message, ErrorCode statusCode, Exception innerException)             : base(String.Format("{0}\r\nStatusCode:{1}", message, statusCode), innerException)         {             this.statusCode = statusCode;         } 

A global supression file is a file in the root of your project named GlobalSuppressions.cs and might look like this:

// This file is used by Code Analysis to maintain SuppressMessage  // attributes that are applied to this project.  // Project-level suppressions either have no target or are given  // a specific target and scoped to a namespace, type, member, etc.  // // To add a suppression to this file, right-click the message in the  // Error List, point to "Suppress Message(s)", and click  // "In Project Suppression File".  // You do not need to add suppressions to this file manually.   [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)", Scope = "member", Target = "Soapi.ApiException.#.ctor(System.String,Soapi.ErrorCode,System.String,System.Exception)")] 

And you can generate this code automatically by right-clicking on the warning.

like image 25
Sky Sanders Avatar answered Sep 24 '22 13:09

Sky Sanders