FxCop FAQ
Following is a collection of Frequently Asked Questions (FAQ) regarding FxCop/Managed Code Analysis answered by members of the team.
Rules
Why does FxCop generate violations against itself?
Answered on the FxCop blog.Why do some sources recommend extending ApplicationException while FxCop does not?
Answered on the FxCop blog.Why does FxCop ignore my in-code (SuppressMessageAttribute) suppressions?
Answered on the FxCop blog.Why does DoNotExposeGenericLists recommend that I expose Collection<T> instead of List<T>?
Answered on the FxCop blog.How do I indicate to DoNotDeclareReadOnlyMutableReferenceTypes that a type is immutable?
Answered on the FxCop blog.Custom Rule Development
Where is the official FxCop custom rules SDK?
[Michael Fanning] There is currently no official custom rules SDK developed by Microsoft. The primary reason is that we are currently rewriting key components of the FxCop metadata reader and data flow analysis engine. This work guarantees that rules written today will need to be modified or entirely re-written in the future. Rules developers who have been working in FxCop for the past few years can attest to the fact that every release has involved some churn in the custom rules API. Each release readme contains detailed information about porting existing rules to the latest architecture.As soon as the FxCop metadata and other relevant API stabilize sufficiently, we will develop an official FxCop custom rules SDK with samples demonstrating common patterns written in several languages. Until then, there are several resources available to custom rules developers:
- This forum. Post your rules development questions here.
- Rules samples uploaded to the www.gotdotnet.com samples area. Browse to http://www.gotdotnet.com/community/usersamples/ and set the Product field to 'FxCop'. Current samples at this location which have been uploaded by Microsoft include:
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=14DEFFCB-9C2B-4C38-BEEC-AB860084E372
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=01D9F014-5F73-4D66-8E74-C658944180DD - Rules samples developed externally. Search the web for 'fxcop' and 'rules'. Here is a starter developed by David Kean (a FxCop team member): http://davidkean.net/archive/2005/02/21/362.aspx
- Visit the custom rules category on the FxCop blog
How do I integrate custom rules with Visual Studio?
Answered on the FxCop blog.How do I access the locals for a method?
Answered on the FxCop blog.
Why can't FxCop visit foreach, while, goto, catch blocks, etc?
[Michael Fanning] FxCop is a binary analysis tool. Its metadata reader raises an abstract syntax tree (AST) from IL, this tree can be accessed from rules by overriding methods on the StandardVisitor class. There are several sample custom rules available that demonstrate this pattern, such as:FxCop Custom rule: demonstrates extracting literal arguments from method call
The StandardVisitor class, however, can be used with a front-end that raises abstract syntax trees directly from source. This is the reason for the existence of methods such as VisitDoWhile, VisitForEach, etc. These methods will never be called within FxCop, since they are not relevant to ASTs raised from IL (source level constructs such as foreach compile away entirely when IL is emitted and cannot be reliably reconstructed).
The existence of these additional methods in classes currently consumed by custom rules is one reason we haven't yet invested in an official SDK. See: Where is the official custom rules SDK?


