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

Add analyzer/fixer to suggest changing code like ImmutableArray.CreateBuilder<int>(); ... to a collection expression. #69500

Merged

Conversation

CyrusNajmabadi
Copy link
Member

Part of #69473.

Looks for code like:

var builder = ImmutableArray.CreateBuilder<int>();
builder.Add(0);
foreach (var y in x)
    builder.Add(y);

Goo(builder.ToImmutable());

And suggests converting to:

Goo([0, .. y]);

This PR Involves a lot of refactoring of hte current analyzer/fixers to more broadly support this scenario. Specifically, the existing 'use collection initializer' already has teh support for analyzing successive statements to see if they're complimentary with a collection-initializer/expression.

But we needed to extract out pieces as that analyzer/fixer assumes you have an initial collection of hte form new Collection<int>(), and it also assumes you're replacing the intitial declaration point (whereas with builders you want to update the place where builder.ToXXX() is called.

@CyrusNajmabadi CyrusNajmabadi merged commit 221323d into dotnet:main Aug 16, 2023
24 checks passed
@ghost ghost added this to the Next milestone Aug 16, 2023
@CyrusNajmabadi CyrusNajmabadi deleted the useCollectionExpressionForBuilder2 branch August 16, 2023 18:20
@dibarbet dibarbet modified the milestones: Next, 17.8 P2 Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants