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

Extend the "Use collection expression" suggestion to scenarios requiring the spread operator #75870

Closed
obonn1 opened this issue Nov 12, 2024 · 4 comments · Fixed by #75879
Closed
Labels
Area-IDE Feature Request help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@obonn1
Copy link
Contributor

obonn1 commented Nov 12, 2024

Summary

Expand the Roslyn analyzers to suggest using [.. collection] syntax where applicable. This is a more concise and expressive way to initialize collection from elements in existing collections.

Background and Motivation

The IDE currently suggests collection initialization when a spread operator wouldn't be required. The motivation for this feature is the same as the existing suggestions, informing developers of the existence of a more readable syntax. Notably, ReSharper already suggests using collection expressions in these situations. Adding this feature directly to Roslyn would ensure consistency and improve native support for modern C# practices.

Proposed Feature

Before:

List<int> GetNumbers()
{
    return new List<int>(Enumerable.Range(1, 10));
}

After:

List<int> GetNumbers()
{
    return [.. Enumerable.Range(1, 10)];
}

Before:

ImmutableArray<string> GetFormattedNumbers(ImmutableArray<int> numbers)
{
    return numbers.Select(n => $"Number: {n}").ToImmutableArray();
}

After:

ImmutableArray<string> GetFormattedNumbers(ImmutableArray<int> numbers)
{
    return [.. numbers.Select(n => $"Number: {n}")];
}

Before:

ImmutableArray<string> GetFormattedRange()
{
    return ImmutableArray.CreateRange(Enumerable.Range(1, 10).Select(n => $"Item {n}"));
}

After:

ImmutableArray<string> GetFormattedRange()
{
    return [.. Enumerable.Range(1, 10).Select(n => $"Item {n}")];
}
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Nov 12, 2024
@CyrusNajmabadi CyrusNajmabadi added help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 12, 2024
@github-project-automation github-project-automation bot moved this to InQueue in Small Fixes Nov 12, 2024
@CyrusNajmabadi CyrusNajmabadi added this to the Backlog milestone Nov 12, 2024
@CyrusNajmabadi
Copy link
Member

We'd likely take targeted community PRs here. Thanks for the suggestions :)

@CyrusNajmabadi
Copy link
Member

Working on this now.

@CyrusNajmabadi
Copy link
Member

@obonn1 Thanks for the suggestions!

@obonn1
Copy link
Contributor Author

obonn1 commented Nov 15, 2024

@CyrusNajmabadi Wow that fast! Thanks for working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature Request help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

2 participants