StyleCop/FxCop 10 - How do you properly suppress a message only on a namespace level?

前端 未结 3 1572
傲寒
傲寒 2021-01-11 17:24

FxCop 10 is complaining about the following:

using XYZ.Blah; //CA1709 - \"XYZ\"
using Xyz.Blah; //No complaint.

using XylophoneSuperDuperLongFullName.Blah;          


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-11 17:30

    MSDN - CA1709: Identifiers should be cased correctly:

    It is safe to suppress this warning if you have your own naming conventions, or if the identifier represents a proper name, for example, the name of a company or a technology.

    You can also add specific terms, abbreviations, and acronyms that to a code analysis custom dictionary. Terms specified in the custom dictionary will not cause violations of this rule. For more information, see How to: Customize the Code Analysis Dictionary.


    That being said, if you feel justified to suppress the message, it really isn't hard at all. In FxCop 10 right click on any message you want to suppress and go to Copy As>Suppress-Message or Copy As>Module-level Suppress Message.

    You should place the SuppressMessageAttributes in the appropriate locations. Attributes that suppress a single location should be placed on that location, for example, above a method, field, property, or class.

    In you're instance, there is no specific location to place the attribute (by default it should copy over as [module: SuppressMessage(...)]. This is a good indication that it belongs either at the top of a file if it is a module-level suppression particular to a file (for example, to a resource specific to a file). Or, and more likely, it belongs in a GlobalSuppressions.cs file.

    using System.Diagnostics.CodeAnalysis;
    
    [module: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Justification = "Because I said so!", MessageId = "XYZ", Scope = "namespace", Target = "XYZ.Blah")]
    

    You can also shorten the CheckId property if you want to, but it's good to know what CA1709 means. If you don't feel like it, this also works:

    using System.Diagnostics.CodeAnalysis;
    
    [module: SuppressMessage("Microsoft.Naming", "CA1709", Justification = "Because I said so!", MessageId = "XYZ", Scope = "namespace", Target = "XYZ.Blah")]
    

    And lastly... all this will be fruitless unless you include the 'CODE_ANALYSIS' symbol in your build. Go to Properties>Build and add the conditional compilation symbol.

提交回复
热议问题