Skip to content

Commit

Permalink
Merge pull request #60822 from DoctorKrolic/fix-codefix-title
Browse files Browse the repository at this point in the history
Changed IDE0059 codefix title in pattern matching
  • Loading branch information
CyrusNajmabadi authored May 3, 2022
2 parents 843db64 + 58acc1a commit c4be259
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8750,6 +8750,28 @@ class C
await TestExactActionSetOfferedAsync(source, new[] { CodeFixesResources.Remove_redundant_assignment });
}

[WorkItem(38507, "https://github.com/dotnet/roslyn/issues/38507")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
public async Task TestCodeFixTitleForPatternMatching()
{
var source = @"
class C
{
void M()
{
var c = M2();
if [|(c is object obj)|]
{
}
}
C M2() => new C();
}
";

await TestExactActionSetOfferedAsync(source, new[] { CodeFixesResources.Remove_redundant_assignment });
}

[WorkItem(38507, "https://github.com/dotnet/roslyn/issues/46251")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
public async Task TestCodeFixForAllInDocumentForNestedDiagnostic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,26 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)

title = CodeFixesResources.Use_discard_underscore;

var syntaxFacts = context.Document.GetRequiredLanguageService<ISyntaxFactsService>();
var root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span, getInnermostNodeForTie: true);

// Check if this is compound assignment which is not parented by an expression statement,
// for example "return x += M();" OR "=> x ??= new C();"
// If so, we will be replacing this compound assignment with the underlying binary operation.
// For the above examples, it will be "return x + M();" AND "=> x ?? new C();" respectively.
// For these cases, we want to show the title as "Remove redundant assignment" instead of "Use discard _".

var syntaxFacts = context.Document.GetRequiredLanguageService<ISyntaxFactsService>();
var root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span, getInnermostNodeForTie: true);
if (syntaxFacts.IsLeftSideOfCompoundAssignment(node) &&
!syntaxFacts.IsExpressionStatement(node.Parent))
{
title = CodeFixesResources.Remove_redundant_assignment;
}
// Also we want to show "Remove redundant assignment" title in pattern matching, e.g.
// if (obj is SomeType someType) <-- "someType" will be fully removed here
else if (syntaxFacts.IsDeclarationPattern(node.Parent))
{
title = CodeFixesResources.Remove_redundant_assignment;
}

break;

Expand All @@ -173,7 +179,6 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
}

RegisterCodeFix(context, title, GetEquivalenceKey(preference, isRemovableAssignment));
return;
}

private static bool IsForEachIterationVariableDiagnostic(Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
Expand Down

0 comments on commit c4be259

Please sign in to comment.