Search code examples
fxcopstatic-code-analysisgendarme

What rule do you wish FxCop/Gendarme had?


What definable static code checking rule do you wish to see added to FxCop and/or Gendarme?

Why to do you wish to see the rule added, e.g what are the benefits etc?

How could your rule be implemented?


Solution

  • Personally, I would prefer to see not using IDisposable implementations in using statements.

    So if you had code like this:

    var fs = new FileStream(...);
    
    // Other code.
    
    fs.Dispose();
    

    It would tell you to use it in a using statement.

    The benefit would be that it would alert you to cases you might not be aware of where objects that should be disposed are not being disposed in a timely manner.

    However, there are enough times where it's a valid situation to NOT declare IDisposable implementations in a using statement for a rule like this to become a pain very quickly. Most often, this case is taking an IDisposable implementation as a parameter to a method.

    What I do not mean is usages of classes where the implementation details remove the need for calling Dispose, (e.g. MemoryStream or DataContext); those implement IDisposable and should always have Dispose called on them, regardless of the implementation details, as it is always better to code against the contract exposed.