Is this a bug in resharper?

114 views Asked by At

I have me some Resharper squiggles here.

enter image description here

and they tell me that I have a possible multiple enumeration of IEnumerable going on. However you can see that this is not true. final is explicitly declared as a list ( List<Point2D> ) and pointTangents is declared previously as List<PointVector2D>

Any idea on why Resharper might be telling me this?

Edit Experiments To See If I can replicate with simpler code

As you can see below there are no squiggles and no warnings even though Bar is declared to take IEnumerable as arg.

enter image description here

2

There are 2 answers

0
AakashM On BEST ANSWER

Looks a lot like RSRP-429474 False-positive warning for possible multiple enumeration :

I have this code:

List<string> duplicateLabelsList = allResourcesLookup.SelectMany(x => x).Select(x => x.LoaderOptions.Label).Duplicates<string, string>().ToList(); ; 
if (duplicateLabelsList.Any()) 
throw new DuplicateResourceLoaderLabelsException(duplicateLabelsList);

For both usages of duplicateLabelsList, I'm being warned about possible multiple enumeration, despite the fact I've called ToList and therefore there should be no multiple enumeration.

which (currently) has a Fix Version of 9.2, which (currently) isn't yet released.

7
InBetween On

The extension method

public static TSource Last<TSource>(this IEnumerable<TSource> source);

is defined for the type IEnumerable<TSource>.

If one looks at the implementation of Last<TSource>:

public static TSource Last<TSource>(this IEnumerable<TSource> source)
{
    if (source == null) throw Error.ArgumentNull("source");
    IList<TSource> list = source as IList<TSource>;
    if (list != null)
    {
         int count = list.Count;
         if (count > 0) return list[count - 1];
    }
    else
    {
         using (IEnumerator<TSource> e = source.GetEnumerator()) 
         {
              if (e.MoveNext())
              {
                    TSource result;
               
                    do
                    {
                       result = e.Current;
                    } while (e.MoveNext());

                    return result;
              }
         }
    }
    throw Error.NoElements();
}

It is clear that if source implements IList then source is not enumerated and therefore your assumption that this is a "bug" in Resharper is correct.

I'd consider it more like a false positive probably due to the fact that Resharper has no general way to know that Last()'s implementation avoids unnecessary enumerations. It is probably deciding to flag the potential multiple enumeration based on the fact that Last<TSource> is defined for typed IEnumerable<T> objects.