Suppressing FxCop warnings for generated code

11/13/08

Suppressing FxCop warnings for generated code

We’ve been using FxCop for quite some time; it’s an excellent static analysis tool. We also use CodeSmith to generate O/RM business objects and enums from the database. Often FxCop rules that are appropriate to developer code are less applicable to generated code. Since assemblies tend to contain mixes of these code types, we have a problem - how do we eliminate the noise from the FxCop reports?

Rather than turning off a bunch of rules or constantly suppressing violations, we found a better solution today.

Add the attribute
[System.CodeDom.Compiler.GeneratedCode("tool", "version")]
to generated classes.

In a CodeSmith template, this becomes:
[System.CodeDom.Compiler.GeneratedCode("CodeSmith", "<%= CodeSmith.Engine.VersionCheck.GetCurrentVersion() %>")]

Set the FxCop project option to “Suppress analysis results against generated code” (Project / Options / Spelling & Analysis).

I didn’t find this in the FxCop documentation, but see this page for more information.

Update - November 26, 2008

This entry in the Code Analysis Team Blog contains important guidelines for using these attributes.


Your Host: webmaster@truewill.net
Copyright © 2000-2013 by William Sorensen. All rights reserved.