Ask a questionAsk a question
 

StickyFxCop FAQ

  • Wednesday, April 05, 2006 4:23 AMDavid M. KeanMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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:

    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?