-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Support converting arrays, whose elements are assigned afterwards, to a collection expression. #69415
Support converting arrays, whose elements are assigned afterwards, to a collection expression. #69415
Conversation
@@ -158,94 +154,11 @@ private static void AnalyzeExplicitStackAllocExpression(SyntaxNodeAnalysisContex | |||
StackAllocArrayCreationExpressionSyntax expression, | |||
CancellationToken cancellationToken) | |||
{ | |||
// has to either be `stackalloc X[]` or `stackalloc X[const]`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a move.
@@ -5,7 +5,7 @@ | |||
using Microsoft.CodeAnalysis.CSharp.Syntax; | |||
using Microsoft.CodeAnalysis.UseCollectionInitializer; | |||
|
|||
namespace Microsoft.CodeAnalysis.UseCollectionExpression; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
calls into new helper that shares logic with stackalloc creation analysis.
} | ||
|
||
return matches.ToImmutable(); | ||
return UseCollectionExpressionHelpers.TryGetMatches( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
calls into the shared helper now.
...rp/CodeFixes/UseCollectionExpression/CSharpUseCollectionExpressionForArrayCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
…seCollectionExpressionForArrayCodeFixProvider.cs
@akhera99 this is ready for review. |
Document document, | ||
ImmutableArray<Diagnostic> diagnostics, | ||
SyntaxEditor editor, | ||
CodeActionOptionsProvider fallbackOptions, | ||
CancellationToken cancellationToken) | ||
{ | ||
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: