提交 004377d8 编写于 作者: M Manish Vasani

Use appropriate code fix title for IDE0059 for unnecessary compound assigment

Fixes #38507
上级 e94cd673
......@@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
......@@ -7487,5 +7488,58 @@ void M(int i)
}
}", options: PreferDiscard);
}
[WorkItem(38507, "https://github.com/dotnet/roslyn/issues/38507")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
public async Task TestCodeFixTitleForBlockBodyRedundantCompoundAssignmentReturn()
{
var source =
@"
<Workspace>
<Project Language=""C#"" CommonReferences=""true"">
<Document>
class C
{
C M(C x)
{
return [|x ??= M2()|];
}
C M2() => new C();
}
</Document>
</Project>
</Workspace>
";
using var testWorkspace = TestWorkspace.Create(source);
var (_, action) = await GetCodeActionsAsync(testWorkspace, parameters: default);
Assert.Equal(FeaturesResources.Remove_redundant_assignment, action.Title);
}
[WorkItem(38507, "https://github.com/dotnet/roslyn/issues/38507")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
public async Task TestCodeFixTitleForExpressionBodyRedundantCompoundAssignmentReturn()
{
var source =
@"
<Workspace>
<Project Language=""C#"" CommonReferences=""true"">
<Document>
class C
{
C M(C x) => [|x ??= M2()|];
C M2() => new C();
}
</Document>
</Project>
</Workspace>
";
using var testWorkspace = TestWorkspace.Create(source);
var (_, action) = await GetCodeActionsAsync(testWorkspace, parameters: default);
Assert.Equal(FeaturesResources.Remove_redundant_assignment, action.Title);
}
}
}
......@@ -100,12 +100,12 @@ internal abstract class AbstractRemoveUnusedValuesCodeFixProvider<TExpressionSyn
SyntaxEditor editor,
ISyntaxFactsService syntaxFacts);
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics[0];
if (!AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.TryGetUnusedValuePreference(diagnostic, out var preference))
{
return Task.CompletedTask;
return;
}
var isRemovableAssignment = AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.GetIsRemovableAssignmentDiagnostic(diagnostic);
......@@ -126,10 +126,27 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
// Do not offer a fix to replace unused foreach iteration variable with discard.
// User should probably replace it with a for loop based on the collection length.
return Task.CompletedTask;
return;
}
title = FeaturesResources.Use_discard_underscore;
// 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.GetLanguageService<ISyntaxFactsService>();
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span, getInnermostNodeForTie: true);
if (syntaxFacts.IsLeftSideOfAnyAssignment(node) &&
!syntaxFacts.IsLeftSideOfAssignment(node) &&
!syntaxFacts.IsExpressionStatement(node.Parent))
{
title = FeaturesResources.Remove_redundant_assignment;
}
break;
case UnusedValuePreference.UnusedLocalVariable:
......@@ -137,7 +154,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
break;
default:
return Task.CompletedTask;
return;
}
}
......@@ -148,7 +165,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
equivalenceKey: GetEquivalenceKey(preference, isRemovableAssignment)),
diagnostic);
return Task.CompletedTask;
return;
}
private static bool IsForEachIterationVariableDiagnostic(Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册