Skip to content

Commit f27c091

Browse files
committed
Properties using the keyword "field" are considered "assignable"
1 parent 21b5f47 commit f27c091

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

samples/Basic.Samples/Basic.Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4+
<LangVersion>preview</LangVersion>
45
<ThinktectureRuntimeExtensions_SourceGenerator_Counter>enabled</ThinktectureRuntimeExtensions_SourceGenerator_Counter>
56
<ThinktectureRuntimeExtensions_SourceGenerator_LogLevel>information</ThinktectureRuntimeExtensions_SourceGenerator_LogLevel>
67
<ThinktectureRuntimeExtensions_SourceGenerator_LogMessageInitialBufferSize>1000</ThinktectureRuntimeExtensions_SourceGenerator_LogMessageInitialBufferSize>

src/Thinktecture.Runtime.Extensions.SourceGenerator/Extensions/TypeSymbolExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ or Constants.Attributes.ValueObject.COMPLEX_NAME
107107
or Constants.Attributes.Union.NAME, // both regular and ad-hoc
108108
ContainingNamespace: { Name: Constants.Attributes.NAMESPACE, ContainingNamespace.IsGlobalNamespace: true }
109109
};
110-
111110
}
112111

113112
public static bool IsMessagePackKeyAttribute([NotNullWhen(true)] this ITypeSymbol? attributeType)
@@ -582,7 +581,7 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
582581
if (field.IsIgnored())
583582
return null;
584583

585-
if (!field.IsReadOnly && !field.IsConst)
584+
if (field is { IsReadOnly: false, IsConst: false })
586585
ReportField(field);
587586

588587
return (field, null);
@@ -604,7 +603,7 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
604603
{
605604
var syntax = (PropertyDeclarationSyntax)property.DeclaringSyntaxReferences.Single().GetSyntax(cancellationToken);
606605

607-
if (syntax.ExpressionBody is not null) // public int Foo => 42;
606+
if (syntax.ExpressionBody is not null) // public int Foo => 42; OR public int Foo => field;
608607
return null;
609608

610609
if (syntax.AccessorList is null)
@@ -633,8 +632,8 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
633632
// public int Foo { get { return 42; } }
634633
// public int Foo { get => 42; }
635634
// public int Foo { get => _foo; }
636-
// If we have 'init' then continue checks
637-
if (!IsDefaultImplementation(getter) && init is null)
635+
// If we have 'init' or use the keyword "field" ({ get => field; }) then continue checks
636+
if (!IsDefaultAccessorOrWithFieldKeyword(getter) && init is null)
638637
return null;
639638

640639
if (property.SetMethod is not null)
@@ -650,7 +649,7 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
650649
}
651650
else if (setter is not null)
652651
{
653-
if (!IsDefaultImplementation(setter))
652+
if (!IsDefaultAccessorOrWithFieldKeyword(setter))
654653
return null;
655654

656655
ReportPropertyMustBeReadOnly(property);
@@ -669,12 +668,13 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
669668
.Select(m => m!.Value);
670669
}
671670

672-
private static bool IsDefaultImplementation(AccessorDeclarationSyntax? accessor)
671+
private static bool IsDefaultAccessorOrWithFieldKeyword(AccessorDeclarationSyntax? accessor)
673672
{
674673
if (accessor is null)
675674
return false;
676675

677-
return accessor.Body is null && accessor.ExpressionBody is null;
676+
return (accessor.Body is null || accessor.Body.DescendantTokens().Any(t => t.IsKind(SyntaxKind.FieldKeyword)))
677+
&& (accessor.ExpressionBody is null || accessor.ExpressionBody.DescendantTokens().Any(t => t.IsKind(SyntaxKind.FieldKeyword)));
678678
}
679679

680680
public static bool HasCreateInvalidItemImplementation(

0 commit comments

Comments
 (0)