これは resharper の誤検知だと思いますが、他の方のご意見を頂きたいです。多分私は何かが欠けています。
この動作は、Resharper 9.2 と 10 の両方で見つかりました。
このクラスを検討してください:
public class Foo
{
public IEnumerable<string> SomeList { get; set; }
}
そして、この方法:
public void Method(Foo thisFooCanBeNull)
{
List<string> theList = thisFooCanBeNull?.SomeList?.ToList();
if (theList != null && theList.Count > 0) //Possible multiple enumeration of IEnumerable
{
foreach (string s in theList) //Possible multiple enumeration of IEnumerable
{
//Do something with list.
}
}
}
theList.Count
とforeach
ワーニングをトリガーしますが、私がしたのでToList()
、これは不可能ですよね?
thisFooCanBeNull?.SomeList?.ToList()
で変更しても警告は発生しませんthisFooCanBeNull.SomeList?.ToList()
が、「この foo は null になる可能性がある」ため、変更したくありません。
このコードに問題がある人はいますか?