From c59d431f47960436034ee5c9f0490c9575d86b17 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Mon, 20 Feb 2017 13:52:11 -0800 Subject: [PATCH] Update Coalesce-for-nullables as well. --- .../UseCoalesceExpressionForNullableTests.cs | 27 +++++++++++++++++++ .../UseCoalesceExpressionTests.vb | 4 +-- ...sceExpressionForNullableCodeFixProvider.cs | 19 ++++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/UseCoalesceExpression/UseCoalesceExpressionForNullableTests.cs b/src/EditorFeatures/CSharpTest/UseCoalesceExpression/UseCoalesceExpressionForNullableTests.cs index 2dc12ff6e87..73931184fce 100644 --- a/src/EditorFeatures/CSharpTest/UseCoalesceExpression/UseCoalesceExpressionForNullableTests.cs +++ b/src/EditorFeatures/CSharpTest/UseCoalesceExpression/UseCoalesceExpressionForNullableTests.cs @@ -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> e = () => [||]!x.HasValue ? y : x.Value; + } +}", +@"using System; +using System.Linq.Expressions; + +class C +{ + void M(int? x, int? y) + { + Expression> e = () => {|Warning:x ?? y|}; + } }"); } } diff --git a/src/EditorFeatures/VisualBasicTest/UseCoalesceExpression/UseCoalesceExpressionTests.vb b/src/EditorFeatures/VisualBasicTest/UseCoalesceExpression/UseCoalesceExpressionTests.vb index 8bd8c55f33f..a955f5ca3b6 100644 --- a/src/EditorFeatures/VisualBasicTest/UseCoalesceExpression/UseCoalesceExpressionTests.vb +++ b/src/EditorFeatures/VisualBasicTest/UseCoalesceExpression/UseCoalesceExpressionTests.vb @@ -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 diff --git a/src/Features/Core/Portable/UseCoalesceExpression/UseCoalesceExpressionForNullableCodeFixProvider.cs b/src/Features/Core/Portable/UseCoalesceExpression/UseCoalesceExpressionForNullableCodeFixProvider.cs index 6f73ad2c463..0a8a3d471c0 100644 --- a/src/Features/Core/Portable/UseCoalesceExpression/UseCoalesceExpressionForNullableCodeFixProvider.cs +++ b/src/Features/Core/Portable/UseCoalesceExpression/UseCoalesceExpressionForNullableCodeFixProvider.cs @@ -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 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(); + var semanticFacts = document.GetLanguageService(); 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 -- GitLab