Skip to content
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 @@ -153,10 +153,15 @@ protected bool TryConvertToExpressionBodyForBaseProperty(
[NotNullWhen(true)] out ArrowExpressionClauseSyntax? arrowExpression,
out SyntaxToken semicolonToken)
{
arrowExpression = null;
semicolonToken = default;

// If we have `X Prop { ... } = ...;` we can't convert this as expr-bodied properties can't have initializers.
if (declaration is PropertyDeclarationSyntax { Initializer: not null })
return false;

if (TryConvertToExpressionBodyWorker(declaration, conversionPreference, cancellationToken, out arrowExpression, out semicolonToken))
{
return true;
}

var getAccessor = GetSingleGetAccessor(declaration.AccessorList);
if (getAccessor?.ExpressionBody != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp.UseExpressionBody;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand All @@ -22,7 +24,10 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.UseExpressionBody;
[Trait(Traits.Feature, Traits.Features.CodeActionsUseExpressionBody)]
public sealed class UseExpressionBodyForPropertiesAnalyzerTests
{
private static async Task TestWithUseExpressionBody(string code, string fixedCode, LanguageVersion version = LanguageVersion.CSharp8)
private static async Task TestWithUseExpressionBody(
[StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string code,
[StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string fixedCode,
LanguageVersion version = LanguageVersion.CSharp8)
{
await new VerifyCS.Test
{
Expand All @@ -38,7 +43,9 @@ private static async Task TestWithUseExpressionBody(string code, string fixedCod
}.RunAsync();
}

private static async Task TestWithUseBlockBody(string code, string fixedCode)
private static async Task TestWithUseBlockBody(
[StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string code,
[StringSyntax(PredefinedEmbeddedLanguageNames.CSharpTest)] string fixedCode)
{
await new VerifyCS.Test
{
Expand Down Expand Up @@ -580,4 +587,40 @@ public long Length //N
""";
await TestWithUseExpressionBody(code, fixedCode);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/77473")]
public async Task TestMissingWithInitializer1()
{
var code = """
class C
{
object Goo
{
get
{
return field;
}
} = new();
}
""";
await TestWithUseExpressionBody(code, code, LanguageVersionExtensions.CSharpNext);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/77473")]
public async Task TestMissingWithInitializer2()
{
var code = """
class C
{
object Goo
{
get
{
return field ??= new();
}
} = new();
}
""";
await TestWithUseExpressionBody(code, code, LanguageVersionExtensions.CSharpNext);
}
}
Loading