这是 resharper 中的错误吗?

问题描述 投票:0回答:2

我这里有一些 Resharper 曲线。

enter image description here

他们告诉我,我正在进行 可能的 IEnumerable 多重枚举。但是你可以看到这不是真的。 final 被明确声明为列表 (

List<Point2D>
) 并且 pointTangents 之前被声明为
List<PointVector2D>

知道为什么 Resharper 会告诉我这个吗?

编辑实验以查看我是否可以使用更简单的代码进行复制

正如您在下面看到的,即使 Bar 被声明为采用 IEnumerable 作为参数,也没有曲线和警告。

enter image description here

c# resharper ienumerable
2个回答
3
投票

看起来很像RSRP-429474可能的多重枚举的误报警告

我有这个代码:

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

对于 duplicateLabelsList 的两种用法,有人警告我 可能的多重枚举,尽管我已经调用了 ToList 和 因此不应该有多重枚举。

(当前)有 9.2 的修复版本,(当前)尚未发布。


-1
投票

扩展方法

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

是为类型

IEnumerable<TSource>
定义的。

如果看一下

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();
}

很明显,如果

source
实现了
IList
,那么
source
就不会被枚举,因此您认为这是 Resharper 中的“错误”的假设是正确的。

我认为它更像是一个误报,可能是因为 Resharper 没有通用的方法知道

Last()
的实现避免了不必要的枚举。它可能决定根据
Last<TSource>
是为类型化的
IEnumerable<T>
对象定义的事实来标记潜在的多重枚举。

© www.soinside.com 2019 - 2024. All rights reserved.