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

[Analyzer] Lift arrays of literals to static fields #78398

Closed
Tracked by #79053
stephentoub opened this issue Nov 15, 2022 · 5 comments
Closed
Tracked by #79053

[Analyzer] Lift arrays of literals to static fields #78398

stephentoub opened this issue Nov 15, 2022 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Milestone

Comments

@stephentoub
Copy link
Member

stephentoub commented Nov 15, 2022

We should consider adding an info-level analyzer that flags cases where arrays of literals are being allocated and recommend they instead be lifted to be stored in static readonly fields. We'll need to experiment a bit to drive down false positive rates, e.g.

  • Don't warn if the array is returned out of the method.
  • Don't warn if the array is mutated by the method.
  • Don't warn if the array is being stored / passed immediately as a ReadOnlySpan<T>.

Related:
#33793

@stephentoub stephentoub added code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer labels Nov 15, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 15, 2022
@ghost
Copy link

ghost commented Nov 16, 2022

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

We should consider adding an info-level analyzer that flags cases where arrays of literals are being allocated and recommend they instead be lifted to be stored in static readonly fields. We'll need to experiment a bit to drive down false positive rates, e.g.

  • Don't warn if the array is returned out of the method.
  • Don't warn if the array is mutated by the method.
  • Don't warn if the array is being stored / passed immediately as a ReadOnlySpan<T>.

Related:
#33793

Author: stephentoub
Assignees: -
Labels:

area-System.Runtime, untriaged, code-analyzer, code-fixer

Milestone: -

@buyaa-n buyaa-n added this to the 8.0.0 milestone Nov 16, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 16, 2022
@stephentoub stephentoub added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 21, 2022
@tannergooding tannergooding modified the milestones: 8.0.0, Future Jul 24, 2023
@buyaa-n
Copy link
Member

buyaa-n commented Mar 12, 2024

Isn't it same as Extract array of const to static readonly field?

@steveberdy
Copy link
Contributor

This definitely looks the same to me

@buyaa-n
Copy link
Member

buyaa-n commented Sep 3, 2024

Yeah, doesn't see any difference, closing as duplicate

@buyaa-n buyaa-n closed this as not planned Won't fix, can't repro, duplicate, stale Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Projects
None yet
Development

No branches or pull requests

5 participants