dotnet / roslyn-analyzers

MIT License
1.58k stars 464 forks source link

CA1851: fails to detect multiple enumeration in certain scenarios #7308

Open hchamre opened 4 months ago

hchamre commented 4 months ago

Analyzer

Diagnostic ID: CA1851: Possible multiple enumerations of IEnumerable collection

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0

Describe the bug

Several cases of multiple enumeration are not detected by the analyzer.

Steps To Reproduce

Preparation:

Expected behavior

Each of the above snippets should generate a "multiple enumeration" diagnostic. Here is my reasoning: 1) Either values is null and then it is never enumerated or values is not null and is enumerated twice. 2) If values.Count() != 0, then values is enumerated twice. 3) values is enumerated twice.

Actual behavior

None of the snippets generate diagnostics.

Additional context

Rider has a similar inspection which is able to flag 1) and 2), but not 3).