提交 c59d431f 编写于 作者: C CyrusNajmabadi

Update Coalesce-for-nullables as well.

上级 a15adaa2
......@@ -184,6 +184,33 @@ void M(int? x, int? y, int? z)
{
var w = x ?? y ?? z;
}
}");
}
[WorkItem(17028, "https://github.com/dotnet/roslyn/issues/17028")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCoalesceExpression)]
public async Task TestInExpressionOfT()
{
await TestAsync(
@"using System;
using System.Linq.Expressions;
class C
{
void M(int? x, int? y)
{
Expression<Func<int>> e = () => [||]!x.HasValue ? y : x.Value;
}
}",
@"using System;
using System.Linq.Expressions;
class C
{
void M(int? x, int? y)
{
Expression<Func<int>> e = () => {|Warning:x ?? y|};
}
}");
}
}
......
......@@ -247,7 +247,7 @@ Imports System.Linq.Expressions
Class C
Sub M(x as string, y as string)
dim e as Expression(of Func(of string)) = [||]If (x isnot Nothing, x, y)
dim e as Expression(of Func(of string)) = func() [||]If (x isnot Nothing, x, y)
End Sub
End Class",
"Imports System
......@@ -255,7 +255,7 @@ Imports System.Linq.Expressions
Class C
Sub M(x as string, y as string, z as string)
dim e as Expression(of Func(of string)) = If (x, y)
dim e as Expression(of Func(of string)) = func () If (x, y)
End Sub
End Class")
End Function
......
......@@ -33,11 +33,15 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return SpecializedTasks.EmptyTask;
}
protected override Task FixAllAsync(
protected override async Task FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
SyntaxEditor editor, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var expressionTypeOpt = semanticModel.Compilation.GetTypeByMetadataName("System.Linq.Expressions.Expression`1");
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var semanticFacts = document.GetLanguageService<ISemanticFactsService>();
var generator = editor.Generator;
var root = editor.OriginalRoot;
......@@ -54,13 +58,20 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
syntaxFacts.GetPartsOfConditionalExpression(
c, out var currentCondition, out var currentWhenTrue, out var currentWhenFalse);
return whenPart == whenTrue
var coalesceExpression = whenPart == whenTrue
? g.CoalesceExpression(conditionExpression, syntaxFacts.WalkDownParentheses(currentWhenTrue))
: g.CoalesceExpression(conditionExpression, syntaxFacts.WalkDownParentheses(currentWhenFalse));
if (semanticFacts.IsInExpressionTree(
semanticModel, conditionalExpression, expressionTypeOpt, cancellationToken))
{
coalesceExpression = coalesceExpression.WithAdditionalAnnotations(
WarningAnnotation.Create(FeaturesResources.Changes_to_expression_trees_may_result_in_behavior_changes_at_runtime));
}
return coalesceExpression;
});
}
return SpecializedTasks.EmptyTask;
}
private class MyCodeAction : CodeAction.DocumentChangeAction
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册