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