views:

309

answers:

5

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 and 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.

A: 

Cant you just remove the rule instead of soiling your code?

Same goes for FxCop...

leppie
I've just added the reason why I can't sole the codes. Please see my edited question.
Nam Gi VU
A: 

If you've installed StyleCop, you can right-click your project and there will be a StyleCop option. Click this and you'll see you can prevent certain rules from even running against your project. Moreover, you can create a separate rules file to share between different projects. This means you can configure the rules once the way you want them and then share that configuration between all your projects.

For individual overrides, SuppressMessage is the way to go.

HTH,
Kent

Kent Boogaart
+5  A: 

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

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..

alt text

Sky Sanders
I've tried right-clicking the warning (in the Error/Warning list) but no context menu item allow me to supress it. I'm using VS 2010. Do you know why?
Nam Gi VU
@Nam - Not sure, the example I cite is using vs2008. let me fire up 2010 and take a peek.
Sky Sanders
@poet: I've post my related question here http://stackoverflow.com/questions/3287957/how-to-supress-stylecop-warning-sa1201-all-methods-must-be-placed-after-all-pro
Nam Gi VU
@Nam - having some issues with vs 2010 - will take me a few hours. If you haven't gotten an good answer by then I will report back. But really it should be quite similar process. till then..
Sky Sanders
@Nam - check the image - as I suspected, the behavior is same. what are you seeing?
Sky Sanders
@poet: Here is the screenshot captured what I see http://dl.dropbox.com/u/6194904/right%20click%20StyleCop%20warning%20but%20no%20Supress%20options.png
Nam Gi VU
hmmm, i guess you cannot use context menu on StyleCop warnings. Those that I show are from FxCop. But - the supression syntax is the same. So I guess your issue is finding the namespace of the rule you are breaking so that you can manually supress, right?
Sky Sanders
@Code Poet: You're absolutely right! I don't know how to put in the parameter for the suppressing commands.
Nam Gi VU
+1  A: 

Here's what you need:

[SuppressMessage("Microsoft.StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess"]

Jason Allor
+1  A: 

Alternatively you could move the code in regions into partial classes. Then the issue with the stylecop rule will go away.

Philip Smith