提交 255faa6f 编写于 作者: P Pär Björklund 提交者: Joey Robichaud

Add support for ??= compound operator to the UseCompound fix (#30639)

Only enabled for C#8 or above to avoid suggesting fixes that
break compilation.
上级 c32b6e73
......@@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.UseCompoundAssignment;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
......@@ -215,6 +216,40 @@ void M(int a)
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestCoalesceExpressionCSharp8OrGreater()
{
await TestInRegularAndScriptAsync(
@"public class C
{
void M(int? a)
{
a [||]= a ?? 10;
}
}",
@"public class C
{
void M(int? a)
{
a ??= 10;
}
}", parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8));
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestCoalesceExpressionCSharp7()
{
await TestMissingAsync(
@"public class C
{
void M(int? a)
{
a [||]= a ?? 10;
}
}",
new TestParameters(parseOptions: new CSharpParseOptions(LanguageVersion.CSharp7_3)));
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestField()
{
......
......@@ -20,5 +20,9 @@ protected override SyntaxKind GetKind(int rawKind)
protected override SyntaxKind GetAnalysisKind()
=> SyntaxKind.SimpleAssignmentExpression;
protected override bool IsSupported(SyntaxKind assignmentKind, ParseOptions options)
=> assignmentKind != SyntaxKind.CoalesceExpression ||
((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp8;
}
}
......@@ -19,7 +19,8 @@ internal static class Utilities
(SyntaxKind.ExclusiveOrExpression, SyntaxKind.ExclusiveOrAssignmentExpression),
(SyntaxKind.BitwiseOrExpression, SyntaxKind.OrAssignmentExpression),
(SyntaxKind.LeftShiftExpression, SyntaxKind.LeftShiftAssignmentExpression),
(SyntaxKind.RightShiftExpression, SyntaxKind.RightShiftAssignmentExpression)).SelectAsArray(
(SyntaxKind.RightShiftExpression, SyntaxKind.RightShiftAssignmentExpression),
(SyntaxKind.CoalesceExpression, SyntaxKind.CoalesceAssignmentExpression)).SelectAsArray(
tuple => (tuple.Item1, tuple.Item2, FindOperatorToken(tuple.Item2)));
private static SyntaxKind FindOperatorToken(SyntaxKind assignmentExpressionKind)
......
......@@ -46,6 +46,7 @@ internal abstract class AbstractUseCompoundAssignmentDiagnosticAnalyzer<
protected abstract TSyntaxKind GetKind(int rawKind);
protected abstract TSyntaxKind GetAnalysisKind();
protected abstract bool IsSupported(TSyntaxKind assignmentKind, ParseOptions options);
public override bool OpenFileOnly(Workspace workspace)
=> false;
......@@ -91,6 +92,12 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
return;
}
// Requires at least C# 8 for Coalesce compound expression
if (!IsSupported(binaryKind, syntaxTree.Options))
{
return;
}
_syntaxFacts.GetPartsOfBinaryExpression(binaryExpression,
out var binaryLeft, out _);
......
......@@ -20,5 +20,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UseCompoundAssignment
Protected Overrides Function GetAnalysisKind() As SyntaxKind
Return SyntaxKind.SimpleAssignmentStatement
End Function
Protected Overrides Function IsSupported(assignmentKind As SyntaxKind, options As ParseOptions) As Boolean
Return True
End Function
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册