From 9be9f492cd814be11e40ff1c8a3da9c8f42b8d10 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Sat, 26 Oct 2024 11:31:59 -0700 Subject: [PATCH] Fix 'use conditional expression' in top level statements --- ...ConditionalExpressionForAssignmentTests.cs | 45 ++++++++++++++++++- ...lExpressionForAssignmentCodeFixProvider.cs | 2 +- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForAssignmentTests.cs b/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForAssignmentTests.cs index b390f823c05cb..548e764d0a344 100644 --- a/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForAssignmentTests.cs +++ b/src/Analyzers/CSharp/Tests/UseConditionalExpression/UseConditionalExpressionForAssignmentTests.cs @@ -19,7 +19,7 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseConditionalExpressio CSharpUseConditionalExpressionForAssignmentCodeFixProvider>; [Trait(Traits.Feature, Traits.Features.CodeActionsUseConditionalExpression)] -public partial class UseConditionalExpressionForAssignmentTests +public sealed partial class UseConditionalExpressionForAssignmentTests { private static async Task TestMissingAsync( string testCode, @@ -2102,4 +2102,47 @@ public static void Test(object obj) } """); } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/71403")] + public async Task TestGlobalStatements() + { + await new VerifyCS.Test + { + TestCode = """ + #nullable enable + + using System; + + object? x = null; + object? y = null; + object? z; + + [|if|] (x != null) + { + z = x; + } + else + { + z = y; + } + + Console.WriteLine($"{x}{y}{z}"); + """, + FixedCode = """ + #nullable enable + + using System; + + object? x = null; + object? y = null; + object? z = x != null ? x : y; + + Console.WriteLine($"{x}{y}{z}"); + """, + LanguageVersion = LanguageVersion.CSharp9, + TestState = { + OutputKind = OutputKind.ConsoleApplication, + } + }.RunAsync(); + } } diff --git a/src/Analyzers/Core/CodeFixes/UseConditionalExpression/ForAssignment/AbstractUseConditionalExpressionForAssignmentCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/UseConditionalExpression/ForAssignment/AbstractUseConditionalExpressionForAssignmentCodeFixProvider.cs index 0c096ac320694..b26a1934237d3 100644 --- a/src/Analyzers/Core/CodeFixes/UseConditionalExpression/ForAssignment/AbstractUseConditionalExpressionForAssignmentCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/UseConditionalExpression/ForAssignment/AbstractUseConditionalExpressionForAssignmentCodeFixProvider.cs @@ -60,7 +60,7 @@ protected override async Task FixOneAsync( SyntaxEditor editor, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken) { var syntaxFacts = document.GetRequiredLanguageService(); - var ifStatement = diagnostic.AdditionalLocations[0].FindNode(cancellationToken); + var ifStatement = diagnostic.AdditionalLocations[0].FindNode(getInnermostNodeForTie: true, cancellationToken); var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); var ifOperation = (IConditionalOperation)semanticModel.GetOperation(ifStatement, cancellationToken)!;