We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Diagnostic ID: CA1851: Possible multiple enumerations of IEnumerable collection
Possible multiple enumerations of IEnumerable collection
NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers
Version: 8.0.0
Several cases of multiple enumeration are not detected by the analyzer.
Preparation:
Create an empty C# project.
Enable NetAnalyzers.
Enable CA1851 in EditorConfig by setting severity to error or warning.
Use the following code snippets:
Multiple enumeration with null-conditional ?.:
?.
IEnumerable<string>? values = SomeMethodReturning<IEnumerable<string>?>(); int? x = values?.Count() + values?.Count();
Multiple enumeration with reassignment in conditional:
IEnumerable<int> values = SomeMethodReturning<IEnumerable<int>>(); if (values.Count() == 0) { values = new List<int> { 123 }; } int x = values.Count();
Multiple enumeration of IQueryable via IEnumerable:
IQueryable
IEnumerable
IEnumerable<int> values = SomeMethodReturning<IQueryable<int>>(); int x = values.Count() + values.Count();
Each of the above snippets should generate a "multiple enumeration" diagnostic. Here is my reasoning:
values
values.Count() != 0
None of the snippets generate diagnostics.
Rider has a similar inspection which is able to flag 1) and 2), but not 3).
The text was updated successfully, but these errors were encountered:
No branches or pull requests
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:
Create an empty C# project.
Enable NetAnalyzers.
Enable CA1851 in EditorConfig by setting severity to error or warning.
Use the following code snippets:
Multiple enumeration with null-conditional
?.
:Multiple enumeration with reassignment in conditional:
Multiple enumeration of
IQueryable
viaIEnumerable
:Expected behavior
Each of the above snippets should generate a "multiple enumeration" diagnostic. Here is my reasoning:
values
is null and then it is never enumerated orvalues
is not null and is enumerated twice.values.Count() != 0
, thenvalues
is enumerated twice.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).
The text was updated successfully, but these errors were encountered: