这是resharper中的错误吗?
Is this a bug in resharper?
我这里有一些 Resharper 曲线。
他们告诉我,我正在进行 可能的 IEnumerable 多重枚举。但是你可以看到这不是真的。 final 被显式声明为列表 ( List<Point2D>
) 并且 pointTangents 之前被声明为 List<PointVector2D>
知道为什么 Resharper 会告诉我这个吗?
编辑实验以查看我是否可以使用更简单的代码进行复制
正如您在下面看到的,即使 Bar 被声明为采用 IEnumerable 作为参数,也没有曲线和警告。
为类型 IEnumberable<TSource>
定义了扩展方法 public static TSource Last<TSource>(this IEnumerable<TSource> source);
。
如果看一下 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 中的 "bug" 正确。
我认为它更像是一个误报,可能是因为 Resharper 没有通用的方法知道 Last()
的实现避免了不必要的枚举.它可能决定根据 Last<TSource>
是为类型化的 IEnumerable<T>
对象定义的事实来标记潜在的多重枚举。
看起来很像 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.
(当前)有 9.2 的修复版本,(当前)尚未发布。
我这里有一些 Resharper 曲线。
他们告诉我,我正在进行 可能的 IEnumerable 多重枚举。但是你可以看到这不是真的。 final 被显式声明为列表 ( List<Point2D>
) 并且 pointTangents 之前被声明为 List<PointVector2D>
知道为什么 Resharper 会告诉我这个吗?
编辑实验以查看我是否可以使用更简单的代码进行复制
正如您在下面看到的,即使 Bar 被声明为采用 IEnumerable 作为参数,也没有曲线和警告。
为类型 IEnumberable<TSource>
定义了扩展方法 public static TSource Last<TSource>(this IEnumerable<TSource> source);
。
如果看一下 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 中的 "bug" 正确。
我认为它更像是一个误报,可能是因为 Resharper 没有通用的方法知道 Last()
的实现避免了不必要的枚举.它可能决定根据 Last<TSource>
是为类型化的 IEnumerable<T>
对象定义的事实来标记潜在的多重枚举。
看起来很像 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.
(当前)有 9.2 的修复版本,(当前)尚未发布。