How to suppress code analysis messages for all type members?

Say I have a listing of all currencies:

public enum CurrencyType { /// <summary> /// United Arab Emirates dirham /// </summary> [EnumMember] AED = 784, /// <summary> /// Afghan afghani /// </summary> [EnumMember] AFN = 971, /// <summary> /// Albanian lek /// </summary> [EnumMember] ALL = 008, ... } 

VS 2015 Code Analysis continues to complain of 100 CA1709 violations for each individual member.

This is a useful rule in itself, and I do not want to disable it; but in this particular case, it doesnโ€™t help much, because CurrencyType is publicly available and is used in many other projects.

I can suppress the message; however, VS only offers to suppress it for each individual participant - this means that I will have 100 lines [SuppressMessage(...)] , which clutters the code.

Is there a way to suppress all CA1709 for all CurrencyType members without suppressing it for all other code in this project, without writing 100 [SuppressMessage(...)] ?

There is a Scope SuppressMessageAttribute parameter, but the documentation on it is unclear. I tried to post both

 [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Scope = "type", Justification = "Currency codes are defined in ISO standard.")] 

and

 [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Scope = "member", Justification = "Currency codes are defined in ISO standard.")] 

on CurrencyType . Does not work.

+13
source share
4 answers

In this case, it is impossible to exclude the rule for the whole class or enumeration, and unfortunately, suppression applies to all its members.

But what you can do is create CodeAnalaysisDictionary.xml , add it to your project containing Enum, and set its "Assembly Property" to CodeAnalysisDictionary :

enter image description here

Once you set this up, you can add abbreviations and case exceptions to the dictionary as follows:

 <Dictionary> <Acronyms> <CasingExceptions> <Acronym>AED</Acronym> <Acronym>AFN</Acronym> <Acronym>ALL</Acronym> <Acronym>...</Acronym> </CasingExceptions> </Acronyms> </Dictionary> 

Although these exceptions will apply to any code element with these acronyms in them, they will prevent CA1709 warnings from occurring.

For more information about exceptions that you can configure using dictionary files, see the documentation:

+8
source

No, there is no way to do this without individual suppression. The Scope argument lets the code analysis engine know what the Target argument is. For example, if the target is โ€œABC,โ€ does this apply to a namespace named ABC or to a class named C in the namespace AB ? It might be better to present Scope with a name like TargetKind, but this, unfortunately, does not change what it actually represents.

Given the ugliness of suppressions in this case, you can generate them in GlobalSuppressions.cs and then move them to a separate file, for example CurrencyTypeMemberNameSuppressions.cs , which you can (optionally) attach as a file to a file containing your CurrencyType enum in the structure of your project in Visual Studio Not perfect, but perhaps the best choice of the bad parties at the moment ...

Also see this answer .

+5
source

what about #pragma warning disable CA1709 ? to reactivate you can use #pragma warning restore CA1709 , but if this enumeration is the only type in your file, you can omit it.

0
source

If your enumeration is in a separate file, you can suppress messages at the class level (for example, MSDN):

 #define CODE_ANALYSIS using System; using System.Diagnostics.CodeAnalysis; namespace CodeAnalysisSample { class Library { [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isChecked")] [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")] static void FileNode(string name, bool isChecked) { string fileIdentifier = name; string fileName = name; string version = String.Empty; } } } 

Check out the following link. this should be useful: https://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute(v=vs.110).aspx

-4
source

All Articles