Skip to content

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

Merged
CyrusNajmabadi merged 69 commits intodotnet:mainfrom
CyrusNajmabadi:useCollectionExpressionForBuilder2
Aug 16, 2023
Merged

Add analyzer/fixer to suggest changing code like ImmutableArray.CreateBuilder<int>(); ... to a collection expression.#69500
CyrusNajmabadi merged 69 commits intodotnet:mainfrom
CyrusNajmabadi:useCollectionExpressionForBuilder2

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

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
@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