This will be a large set of work items. So i'm breaking things into pieces to make it easier to review. The pieces are at least:
[x] Convert arrays and implicit array creation with initializers over to collection expressions. e.g. int[] x = { 1, 2, 3 } => int[] x = [1, 2, 3];. https://github.com/dotnet/roslyn/pull/69118
[x] Update our existing 'use collection initializer' analyzer/fixer to detect 'AddRange' calls and update to ... e.g. x.Add(1); x.AddRange(y) => [1, ..y]. https://github.com/dotnet/roslyn/pull/69219
[x] Update our existing 'use collection initializer' analyzer/fixer to detect when it can jump from the starting point directly to an collection expression. https://github.com/dotnet/roslyn/pull/69321
[x] Convert arrays that are constructed, and then assigned with statements over to collection expressions. e.g. var x = new string[3]; x[0] = ""; ...https://github.com/dotnet/roslyn/pull/69415
[x] Convert collections that have the 'CollectionBuilderAttribute'. Look for usages of things like ImmutableArray.Create(1, 2, 3) => [1, 2, 3]. https://github.com/dotnet/roslyn/pull/69473
[x] Convert collections that have the 'CollectionBuilderAttribute'. Look for usages of things like ImmutableArray.CreateBuilder(); builder.Add ... Support Add/AddRange/etc. https://github.com/dotnet/roslyn/pull/69500
[ ] Inline arrays. Likely not impactful enough to warrant special fixes here.
The intent here is to also provide different diagnostic IDs for varying classes of these. For example "for arrays", "for spans", etc. We may want to revise this as appropriate as we continue with this.
Cases to support:
This will be a large set of work items. So i'm breaking things into pieces to make it easier to review. The pieces are at least:
int[] x = { 1, 2, 3 }
=>int[] x = [1, 2, 3];
. https://github.com/dotnet/roslyn/pull/69118..
. e.g.x.Add(1); x.AddRange(y)
=>[1, ..y]
. https://github.com/dotnet/roslyn/pull/69219Array.Empty<int>()
andImmutableXXX<int>.Empty
. https://github.com/dotnet/roslyn/pull/69279if (x) y.Add(z)
=>.. x ? [z] : []
. https://github.com/dotnet/roslyn/pull/69280new List<int>{ 1, 2, 3 }
=>[1, 2, 3]
. https://github.com/dotnet/roslyn/pull/69321..
. https://github.com/dotnet/roslyn/pull/69321var x = new string[3]; x[0] = ""; ...
https://github.com/dotnet/roslyn/pull/69415ImmutableArray.Create(1, 2, 3)
=>[1, 2, 3]
. https://github.com/dotnet/roslyn/pull/69473ImmutableArray.CreateBuilder(); builder.Add ...
Support Add/AddRange/etc. https://github.com/dotnet/roslyn/pull/69500a.Concat(b).ToImmutableArray()
=>[..a, ..b]
. https://github.com/dotnet/roslyn/pull/69580Stretch cases:
The intent here is to also provide different diagnostic IDs for varying classes of these. For example "for arrays", "for spans", etc. We may want to revise this as appropriate as we continue with this.
Relates to test plan https://github.com/dotnet/roslyn/issues/66418 (collection expressions compiler feature)