Skip to content
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

Subtype relationship between collection and read-only collection interfaces impacts behavior of analyzers #7305

Open
eiriktsarpalis opened this issue Apr 25, 2024 · 5 comments
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers help wanted The issue is up-for-grabs, and can be claimed by commenting
Milestone

Comments

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Apr 25, 2024

Cc @tannergooding @eiriktsarpalis on the error CA1826: (NETCORE_ENGINEERING_TELEMETRY=Build) Do not use Enumerable methods on indexable collections. Instead use the collection directly. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1826) failures ("This rule analyzes collection types that implement IReadOnlyList<T> but not IList<T>.")

Originally posted by @MichalStrehovsky in dotnet/installer#19577 (comment)

@hez2010
Copy link

hez2010 commented Apr 25, 2024

It seems like an outdated analyzer rule which should be disabled.

@tannergooding
Copy link
Member

Just noting that this was discussed a bit offline...

The general intent of CA1826 has been to catch places where x.First() or similar calls would not be specal cased and therefore would fallback to the slower behavior where it actually enumerates, which up until this point has historically included IReadOnlyList<T> and similar interfaces, but not IList<T> itself (which most LINQ APIs explicitly special cased).

With the new change, IList<T> implements IReadOnlyList<T> by default now and because ASP.NET has overridden it's severity from suggestion to warning and enabled warnaserror, it's triggering unnecessarily in more places.

https://github.com/dotnet/runtime/pull/101469/files#diff-dcec95b9c555c8a762091e5ed283baa292291e6fb5af994ecb8d464c4a5fa5b4L81 will effectively obsolete the analyzer on modern .NET, as IROList<T> will be the specialized type and will include the prior set of IList<T>, so it's something we'll likely change to just not run for modern TFMs (or tweak the logic around what exactly its intent is and when/where it should fire). -- CC. @stephentoub

@CyrusNajmabadi
Copy link
Member

This should move to roslyn-anlayzers for tracking?

@eiriktsarpalis
Copy link
Member Author

We track at least some analyzer issues in this repo. I defer to @buyaa-n on picking the appropriate location.

@buyaa-n
Copy link
Contributor

buyaa-n commented Apr 26, 2024

As it is not a new rule that needs API review moving to roslyn-analyzers repo

@buyaa-n buyaa-n transferred this issue from dotnet/runtime Apr 26, 2024
@buyaa-n buyaa-n added Area-Microsoft.CodeAnalysis.NetAnalyzers help wanted The issue is up-for-grabs, and can be claimed by commenting labels Apr 26, 2024
@buyaa-n buyaa-n added this to the .NET vNext milestone Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

No branches or pull requests

5 participants