New issue
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
Enable Code Quality rule CA1851 #20996
Conversation
fba78aa
to
5bf18d4
Compare
is ToList better for perf than a ToArray? |
So as a rule of thumb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, went through the changes and also ran a large AI game
Needs a rebase |
5bf18d4
to
ec61e0b
Compare
This does not appear in VS Code nor with |
It's working for me locally, my What SDK version are you running locally? And if you update to at least the same version as CI does that improve the situation? |
The docs for CA1851 also state it was introduced in .NET 7. A list of rules introduced in .NET 7 is here. https://github.com/dotnet/roslyn-analyzers/blob/main/src/NetAnalyzers/Core/AnalyzerReleases.Shipped.md#release-70 If it turns out you actually need a .NET 7 SDK for the rule then that would explain the gap. |
It does need a .NET 7 SDK. The Windows CI installs 6 and won't flag if the rule is violated. The Linux CI runner comes with 7, so installing 6 is a no-op, and it flags if the rule is violated. |
Following on from #20957
Enforces https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1851
We fix violations in a few ways:
IReadOnlyCollection<T>
. This reduces the flexibility of inputs, but often input is already a materialized collection and thus this isn't a problem in many callsites. We can safely enumerate a collection multiple times.ToList
. This means allocating an intermediate list but is always an option if the above options cannot be employed.Specific changes of interest:
IReadOnlyCollection
which helps a lot of callsites not have to deal withIEnumerable
.