Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'use conditional expression' in top level statements #75645

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading