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:
Convert arrays and implicit array creation with initializers over to collection expressions. e.g. int[] x = { 1, 2, 3 } => int[] x = [1, 2, 3];. Initial support for a set of analyzers/fixers to update existing code to use collection expressions. #69118
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]. Update "use collection initializer" to use a collection-expression if the user prefers that form. #69219
Direct calls to methods like Array.Empty<int>() and ImmutableXXX<int>.Empty. Add analyzer/fixer to update expressions like Array.Empty<T>() over to using the [] collection expression. #69279
Detect if (x) y.Add(z) => .. x ? [z] : []. Add support for generating the .. x ? [y] : [] pattern in a collection-expression #69280
Convert collection initializers. e.g. new List<int>{ 1, 2, 3 } => [1, 2, 3]. Add support for converting existing collection-initializers over to collection expressions. #69321
Update our existing 'use collection initializer analyzer/fixer to detect a foreach+Add call and update to ... Add support for converting existing collection-initializers over to collection expressions. #69321
Update our existing 'use collection initializer' analyzer/fixer to detect when it can jump from the starting point directly to an collection expression. Add support for converting existing collection-initializers over to collection expressions. #69321
Convert stackalloc'ed spans. Add analyzer/fixer to recommend updating stackalloc expressions to use collection expressions. #69414
Convert arrays that are constructed, and then assigned with statements over to collection expressions. e.g. var x = new string[3]; x[0] = ""; ... Support converting arrays, whose elements are assigned afterwards, to a collection expression. #69415
Convert collections that have the 'CollectionBuilderAttribute'. Look for usages of things like ImmutableArray.Create(1, 2, 3) => [1, 2, 3]. Add analyzer/fixer to suggest changing code like ImmutableArray.Create(1, 2, 3) to [1, 2, 3] #69473
Convert collections that have the 'CollectionBuilderAttribute'. Look for usages of things like ImmutableArray.CreateBuilder(); builder.Add ... Support Add/AddRange/etc. Add analyzer/fixer to suggest changing code like ImmutableArray.CreateBuilder<int>(); ... to a collection expression. #69500
Convert arrays converted to spans with .AsSpan(). Add analyzer/fixer to suggest changing code fluent collection building to a collection expression #69580
Convert collections created with linq-like helpers. a.Concat(b).ToImmutableArray() => [..a, ..b]. Add analyzer/fixer to suggest changing code fluent collection building to a collection expression #69580
Stretch 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 #66418 (collection expressions compiler feature)
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];. Initial support for a set of analyzers/fixers to update existing code to use collection expressions. #69118... e.g.x.Add(1); x.AddRange(y)=>[1, ..y]. Update "use collection initializer" to use a collection-expression if the user prefers that form. #69219Array.Empty<int>()andImmutableXXX<int>.Empty. Add analyzer/fixer to update expressions likeArray.Empty<T>()over to using the[]collection expression. #69279if (x) y.Add(z)=>.. x ? [z] : []. Add support for generating the.. x ? [y] : []pattern in a collection-expression #69280new List<int>{ 1, 2, 3 }=>[1, 2, 3]. Add support for converting existing collection-initializers over to collection expressions. #69321... Add support for converting existing collection-initializers over to collection expressions. #69321var x = new string[3]; x[0] = ""; ...Support converting arrays, whose elements are assigned afterwards, to a collection expression. #69415ImmutableArray.Create(1, 2, 3)=>[1, 2, 3]. Add analyzer/fixer to suggest changing code likeImmutableArray.Create(1, 2, 3)to[1, 2, 3]#69473ImmutableArray.CreateBuilder(); builder.Add ...Support Add/AddRange/etc. Add analyzer/fixer to suggest changing code likeImmutableArray.CreateBuilder<int>(); ...to a collection expression. #69500a.Concat(b).ToImmutableArray()=>[..a, ..b]. Add analyzer/fixer to suggest changing code fluent collection building to a collection expression #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 #66418 (collection expressions compiler feature)