Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I suppress FX Cop code analysis violations globally?

When you use Visual Studio's code analysic (FxCop), and want to suppress a message there are 3 options.

  1. Suppress a violation in code.
  2. Suppress a violation in a GlobalSupression.cs file.
  3. Disable the violation check in the project file (via Project -> Properties -> Code Analysic).

The later is very hard to review when checking in to Source Control, and it is hard to get an overview of all disabled violations. So we would like to use option 2.

The problem with option 1 and 2 is that you get one suppression line for each violation. E.g like:

[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Company.Project.Namespace2")]
[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Company.Project.Namespace1")]

We would love to do something like this ing GlobalSuppressions.cs:

[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes")]

But is this possible?

like image 555
Thomas Jespersen Avatar asked Sep 16 '08 14:09

Thomas Jespersen


People also ask

How do you suppress Code Analysis warnings?

You can suppress violations in code using a preprocessor directive, the #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code. Or, you can use the SuppressMessage attribute.

How do I stop live Code Analysis?

To open this page, right-click the project node in Solution Explorer and select Properties. Select the Code Analysis tab. To disable source analysis at build time, uncheck the Run on build option. To disable live source analysis, uncheck the Run on live analysis option.

How do I disable Fxcop?

Right click on your project node in Solution Explorer, choose Properties. There is a Code Analysis tab near the bottom. Click that and uncheck "Enable Code Analysis on Build".


2 Answers

Suppressing multiple violations with a single SuppressMessage attribute is officially not supported. Apparently, this is by design.

I agree, it might be annoying at times, but I can't say I disagree with the decision, since the attribute is their way to force you to say, "Yes, I know what I am doing", which should be evaluated in a case-by-case basis.

like image 122
Ishmaeel Avatar answered Sep 21 '22 15:09

Ishmaeel


I think things have changed since this question was posted and answered. For Visual Studio 2010 and 2012 you can create a custom "rule set" file where you can specify which code analysis rules you want to suppress.

http://msdn.microsoft.com/en-us/library/dd380660.aspx

So what I've done is to create a single custom rule set file which is in the top level folder of my repository source collection, and I reference this file in each Visual Studio project. This means I have one central place where I can suppress the code analysis rules that I simply can't stand. But if I ever change my mind, or decide I should reconsider my bad coding habits, I can very simply reenable the rule and see how many code analysis messages I get.

like image 43
RenniePet Avatar answered Sep 19 '22 15:09

RenniePet