Support converting arrays, whose elements are assigned afterwards, to a collection expression.#69415
Conversation
| StackAllocArrayCreationExpressionSyntax expression, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| // has to either be `stackalloc X[]` or `stackalloc X[const]`. |
There was a problem hiding this comment.
moved to common helper code, since we'll use it for stackalloc and new array creations.
| CancellationToken cancellationToken) | ||
| where TArrayCreationExpressionSyntax : ExpressionSyntax | ||
| { | ||
| Contract.ThrowIfFalse(expression is ArrayCreationExpressionSyntax or StackAllocArrayCreationExpressionSyntax); |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.UseCollectionInitializer; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.UseCollectionExpression; |
There was a problem hiding this comment.
a few types were in the wrong namespace. fixed up with downstream consequences.
| expression, | ||
| static e => e.Type, | ||
| static e => e.Initializer, | ||
| cancellationToken); |
There was a problem hiding this comment.
calls into new helper that shares logic with stackalloc creation analysis.
| } | ||
|
|
||
| return matches.ToImmutable(); | ||
| return UseCollectionExpressionHelpers.TryGetMatches( |
There was a problem hiding this comment.
calls into the shared helper now.
...rp/CodeFixes/UseCollectionExpression/CSharpUseCollectionExpressionForArrayCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
…seCollectionExpressionForArrayCodeFixProvider.cs
|
@akhera99 this is ready for review. |
| CodeActionOptionsProvider fallbackOptions, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); |
There was a problem hiding this comment.
fixed this up to follow the pattern we use for all the rest of the rewriters. important, this handles successive rewrites with arrays within arrays.
There was a problem hiding this comment.
trying to decide if this pattern should be extracted out somewhere as it's now done in like 4 places. may do a followup where that happens.
| { | ||
| void M(int i, int j) | ||
| { | ||
| int[][] r = [[1], [2]]; |
There was a problem hiding this comment.
demonstrates recursive fixall working.
|
@akhera99 ptal :) |
Part of #69132
Followup to #69414. That PR should go in first before reviewing this.
Updates the cases we handle to support: