tags:

views:

1130

answers:

5

Is it possible to exclude a complete namespace from all FxCop analysis while still analyzing the rest of the assembly using the SuppressMessageAttribute?

In my current case, I have a bunch of classes generated by LINQ to SQL which cause a lot of FxCop issues, and obviously, I will not modify all of those to match FxCop standards, as a lot of those modifications would be gone if I re-generated the classes.

I know that FxCop has a project option to suppress analysis on generated code, but it does not seem to recognize the entity and context classes created by LINQ 2 SQL as generated code.

A: 

The code generate with LINQ2SQL is already decorated with a GeneratedCodeAttribute. FXCop is meant to exclude such code from analysis.

leppie
There is no such attribute on my LINQ 2 SQL code. I just created it using the visual designer and that is it. Could it be that they changed it with VS2008 SP1? I am still on VS2008 here.
hangy
A: 

Use the Generated Code Attribute, heres the blog post from the Code Analysis team on the subject.

This at the top of the namespace should do the trick:

[GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
Chane
Unfortunately, one cannot add those attributes to namespaces, as far as I know. That would mean I would have to add it to each generated class manually - that cannot be the ideal solution. :D
hangy
pretty much. The real pain is if you have to regenerate the code for any reason you have to drop the attribute on again.If the classes Linq to Sql generates are partials, you can maintain the attributes away from the generated code. I have a similar issue with EF classse.
Chane
+1  A: 
<Assembly: SuppressMessage("Microsoft.Design", _
    "CA1020:AvoidNamespacesWithFewTypes", _
    Scope:="namespace", _
    Target:="Missico.IO")> 

Put statement in GlobalSuppressions.vb at root of project.

All I have is VB example.

AMissico
This doesn't work for many of the code generation warnings in VS 2010 Code Analysis: don't seem to be suppressible at namespace level. But there is another option: http://stackoverflow.com/questions/2221881/code-analysis-on-a-code-generator-generated-file-how-to-suppress-warnings/2739791#2739791
Richard
+4  A: 

Add a [GeneratedCode] attribute to the classes.
EDIT: I meant to partial classes with the same names, as explained by the other answer.

SLaks
-1 only because you don't want to add it to the generated class, you want to create a partial class and add the attribute to that.
George Stocker
@George: That's what I meant.
SLaks
+4  A: 

If you tag your classes with the [GeneratedCode] attribute, you can use the /ignoregeneratedcode flag with FxCop as described in this MSDN post:

FAQ: How do I prevent FxCop from firing warnings against generated code

You may have to add a new code file and implement new partial classes there to add the attribute to the classes:

[GeneratedCode]
public partial class MainDataContext { }

Just make sure you add everything to the correct namespace when you create your new file.

Justin Niessner
+1 with generated code, this is the correct way to do it.
George Stocker
Genius - thank you, sir
Keith Williams