views:

526

answers:

4

We run Code Analysis on all our source files. One of our source files is a Linq-To-SQL generated file, which we have no control over the generated output. The generated code is producing Code Analysis warnings that I would like to suppress. Is there any way I can suppress CA warnings in a code generated file that doesn't involve creating attributes and/or pragma's in the code itself (which will get overwritten each time the file is generated)?

Randy

+5  A: 

Do your classes have the [GeneratedCode] attribute? If so you can get FxCop to ignore them:

Using an FxCop project:

  1. Open your FxCop project in FxCop
  2. Choose Project -> Options -> Spelling & Analysis
  3. Check Suppress analysis results against generated code
  4. Click OK

Via the command-line:

  1. Pass the /ignoregeneratedcode switch, for example:
     FxCopCmd.exe /file:MyAssembly.dll /out:AnalysisResults.xml /ignoregeneratedcode

http://blogs.msdn.com/fxcop/archive/2008/02/28/faq-how-do-i-prevent-fxcop-1-36-from-firing-warnings-against-generated-code.aspx

Paolo
A: 

If you are using the FxCop GUI you could simply exclude these issues within the FxCop project. Just right click the issue and select Exclude where you can than also add a comment.
But if you run FxCop in the Output Window I do not have a clue. Maybe you could check if it is possible to create a module-level SuppressMessage and paste it into the AssemblyInfo.cs. But I don't think so.

Simon Linder
there's another answer in a related question here on SO that indicates that you can add the module-level suppression to assemblyinfo.cs, but I just tried it and couldn't get it to work. I could add SuppressMessage to every individual method, but that's a total pain in the neck when I want to ignore the error for everything in a particular assembly. Hopefully, someone will answer with a solution to this!!!
Dave
+1  A: 

You can work around the lack of GeneratedCode attribute by using your own branch of the partial classes to apply that attribute. This will mean that any custom code you add (including implementing partial methods) will be excluded. Eg.:

namespace MyApp.DB {
    [GeneratedCode("LINQ To SQL", "4.0")]
    internal partial class MyAppDataContext {
    }

    // Repeat for each entity
}
Richard
A: 

Hello,

The PLINQO (Linq-to-SQL) CodeSmith templates also generate this attribute for you automatically. Most add-in's are also starting to ignore partial classes that are generated with ".generated" in the filename.

[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "5.0.0.0")]

Thanks -Blake Niemyjski

Blake Niemyjski