Skip to content

Commit

Permalink
Merge pull request #75645 from CyrusNajmabadi/useConditionalTopLevel
Browse files Browse the repository at this point in the history
Fix 'use conditional expression' in top level statements
  • Loading branch information
CyrusNajmabadi authored Oct 29, 2024
2 parents 57f05a3 + 9be9f49 commit 1105596
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override async Task FixOneAsync(
SyntaxEditor editor, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>();
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)!;
Expand Down

0 comments on commit 1105596

Please sign in to comment.