Skip to content

Commit

Permalink
Don't crash Use ExpressionBody on local functions in top level statem…
Browse files Browse the repository at this point in the history
…ents (dotnet#57571)
  • Loading branch information
davidwengier authored and jmarolf committed Nov 17, 2021
1 parent 8931402 commit 1262b16
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -90,7 +89,7 @@ private static void AddEdits(
{
var declarationLocation = diagnostic.AdditionalLocations[0];
var helper = _helpers.Single(h => h.DiagnosticId == diagnostic.Id);
var declaration = declarationLocation.FindNode(cancellationToken);
var declaration = declarationLocation.FindNode(getInnermostNodeForTie: true, cancellationToken);
var useExpressionBody = diagnostic.Properties.ContainsKey(nameof(UseExpressionBody));

var updatedDeclaration = helper.Update(semanticModel, declaration, useExpressionBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.UseExpressionBody;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Testing;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseExpressionBody
Expand Down Expand Up @@ -829,5 +831,70 @@ void Bar()
}";
await TestWithUseExpressionBody(code, fixedCode);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExpressionBody)]
[WorkItem(57570, "https://github.com/dotnet/roslyn/issues/57570")]
public async Task TestUseExpressionBodyTopLevelStatment()
{
await new VerifyCS.Test
{
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources =
{
@"
{|IDE0061:int Bar(int x)
{
return x;
}|}
"
},
},
FixedState =
{
Sources =
{
@"
int Bar(int x) => x;
"
},
},
LanguageVersion = LanguageVersion.CSharp9,
Options = { { CSharpCodeStyleOptions.PreferExpressionBodiedLocalFunctions, ExpressionBodyPreference.WhenPossible } },
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExpressionBody)]
[WorkItem(57570, "https://github.com/dotnet/roslyn/issues/57570")]
public async Task TestUseBlockBodyTopLevelStatment()
{
await new VerifyCS.Test
{
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources =
{
@"
{|IDE0061:int Bar(int x) => x;|}
"
},
},
FixedState =
{
Sources =
{
@"
int Bar(int x)
{
return x;
}"
},
},
LanguageVersion = LanguageVersion.CSharp9,
Options = { { CSharpCodeStyleOptions.PreferExpressionBodiedLocalFunctions, ExpressionBodyPreference.Never } },
}.RunAsync();
}
}
}

0 comments on commit 1262b16

Please sign in to comment.