I’m coming up to speed with Gendarme, the open source assembly inspector.

Mostly, the tool seems to deliver on its promise, but I’m having a problem with a false positive.

The EnsureLocalDisposalRule rule is intended to flag situations where Disposable instances aren’t properly cleaned up.

Sounds great - except the rule is triggering on code where I don’t (can’t?) see anything to fix …

Check out this example:

/// <summary>
/// Make replace selected items in an enumerated sequence 
/// </summary>
/// <typeparam name="T">Type of instance in the sequence</typeparam>
/// <param name="source">Original sequence to modify</param>
/// <param name="selector">Predicate selecting items to replace</param>
/// <param name="replacment">Converter supplying replacement items</param>
/// <returns></returns>

public static IEnumerable<T> ReplaceSelected<T>(
    this IEnumerable<T> source,
    Predicate<T> selector,
    Converter<T, T> replacment)
    foreach(var item in source)
        if (selector(item))
            yield return replacment(item);
            yield return item;

I’ve thought that the foreach statement automatically disposed of the enumeration when iteration completed.

In fact, the MSDN page “Using foreach with Collections” says:

The foreach statement provides support for disposable enumerators. If an enumerator implements the IDisposable interface, the foreach statement guarantees that the Dispose method is called on the enumerator no matter how the enumeration loop is terminated.

So, surely this Gendarme rule is a false positive?


blog comments powered by Disqus
Next Post
An Event Snippet  17 May 2009
Prior Post
WinForms DataBinding Goodness  27 Apr 2009
Related Posts
Dependency Load Failures  29 Apr 2011
Synergy  20 Jun 2009
StyleCop for ReSharper  24 Feb 2009
Reflector  28 Aug 2008
Sequence Diagrams for Reflector  23 Jun 2008
NAnt Naming  22 Apr 2008
Insanity, revisited  13 Sep 2004
Visual Basic is driving me insane  25 Aug 2004
Wiki for the win  01 Dec 2003
May 2009