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);
        }
        else
        {
            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?

Comments

blog comments powered by Disqus
Next Post
An Event Snippet  17 May 2009
Prior Post
WinForms DataBinding Goodness  27 Apr 2009
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
May 2009
2009