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

Add analyzer/fixer to update expressions like Array.Empty<T>() over to using the [] collection expression. #69279

Merged
merged 23 commits into from
Jul 31, 2023

Conversation

CyrusNajmabadi
Copy link
Member

Draft while waiting for #69219 to go in.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 28, 2023
@CyrusNajmabadi CyrusNajmabadi requested a review from akhera99 July 28, 2023 19:45
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review July 28, 2023 20:27
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner July 28, 2023 20:27
@@ -12,33 +12,30 @@
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.UseCollectionInitializer;

namespace Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer
namespace Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review with whitespasce off.

@@ -53,7 +54,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)

foreach (var diagnostic in diagnostics.OrderByDescending(d => d.Location.SourceSpan.Start))
{
var expression = diagnostic.Location.FindNode(getInnermostNodeForTie: true, cancellationToken);
var expression = diagnostic.AdditionalLocations[0].FindNode(getInnermostNodeForTie: true, cancellationToken);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a bug before, but was not something you could generally notice as the location was the first token of these nodes and asking for the node would give you teh same thing. but htis is more correct/safer, so i updated.

IsOnSingleLine(sourceText, initializer)));
}

bool ShouldReplaceExistingExpressionEntirely(ExpressionSyntax explicitOrImplicitArray, InitializerExpressionSyntax initializer)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is from #69278

@@ -243,7 +243,8 @@ class C
FixedCode = """
class C
{
object[] i = [
object[] i =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is from #69278

using Microsoft.CodeAnalysis.Testing;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.Analyzers.UnitTests.UseCollectionExpression;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are the new tests.

@CyrusNajmabadi CyrusNajmabadi force-pushed the useCollectionExpression3 branch from 01a4e2b to 1534a1a Compare July 29, 2023 03:49
@CyrusNajmabadi
Copy link
Member Author

@akhera99 ptal :)

@CyrusNajmabadi CyrusNajmabadi requested review from genlu and Cosifne July 31, 2023 17:01
@CyrusNajmabadi
Copy link
Member Author

@genlu @Cosifne ptal

@@ -88,14 +88,14 @@ private static void AnalyzeArrayInitializer(SyntaxNodeAnalysisContext context)
: initializer;

if (!UseCollectionExpressionHelpers.CanReplaceWithCollectionExpression(
semanticModel, arrayCreationExpression, cancellationToken))
semanticModel, arrayCreationExpression, skipVerificationForReplacedNode: false, cancellationToken))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed as the speculative system currently isn't ok with a node's kind changing this dramatically. specifically from a method invocation to another expression form. I wasn't comfortable mucking with the deep speculatrion logic, so easier to just not compare the bottommost nodes directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄I don't have any context about this, but choose to trust you here

Copy link
Member

@Cosifne Cosifne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for the new analyzer/test part.

@CyrusNajmabadi CyrusNajmabadi merged commit 18d7cf9 into dotnet:main Jul 31, 2023
@CyrusNajmabadi CyrusNajmabadi deleted the useCollectionExpression3 branch July 31, 2023 22:28
@ghost ghost added this to the Next milestone Jul 31, 2023
@dibarbet dibarbet modified the milestones: Next, 17.8 P2 Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants