@@ -107,7 +107,6 @@ or Constants.Attributes.ValueObject.COMPLEX_NAME
107
107
or Constants . Attributes . Union . NAME , // both regular and ad-hoc
108
108
ContainingNamespace : { Name : Constants . Attributes . NAMESPACE , ContainingNamespace . IsGlobalNamespace : true }
109
109
} ;
110
-
111
110
}
112
111
113
112
public static bool IsMessagePackKeyAttribute ( [ NotNullWhen ( true ) ] this ITypeSymbol ? attributeType )
@@ -582,7 +581,7 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
582
581
if ( field . IsIgnored ( ) )
583
582
return null ;
584
583
585
- if ( ! field . IsReadOnly && ! field . IsConst )
584
+ if ( field is { IsReadOnly : false , IsConst : false } )
586
585
ReportField ( field ) ;
587
586
588
587
return ( field , null ) ;
@@ -604,7 +603,7 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
604
603
{
605
604
var syntax = ( PropertyDeclarationSyntax ) property . DeclaringSyntaxReferences . Single ( ) . GetSyntax ( cancellationToken ) ;
606
605
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;
608
607
return null ;
609
608
610
609
if ( syntax . AccessorList is null )
@@ -633,8 +632,8 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
633
632
// public int Foo { get { return 42; } }
634
633
// public int Foo { get => 42; }
635
634
// 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 )
638
637
return null ;
639
638
640
639
if ( property . SetMethod is not null )
@@ -650,7 +649,7 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
650
649
}
651
650
else if ( setter is not null )
652
651
{
653
- if ( ! IsDefaultImplementation ( setter ) )
652
+ if ( ! IsDefaultAccessorOrWithFieldKeyword ( setter ) )
654
653
return null ;
655
654
656
655
ReportPropertyMustBeReadOnly ( property ) ;
@@ -669,12 +668,13 @@ void ReportPropertyInitAccessorMustBePrivate(IPropertySymbol property)
669
668
. Select ( m => m ! . Value ) ;
670
669
}
671
670
672
- private static bool IsDefaultImplementation ( AccessorDeclarationSyntax ? accessor )
671
+ private static bool IsDefaultAccessorOrWithFieldKeyword ( AccessorDeclarationSyntax ? accessor )
673
672
{
674
673
if ( accessor is null )
675
674
return false ;
676
675
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 ) ) ) ;
678
678
}
679
679
680
680
public static bool HasCreateInvalidItemImplementation (
0 commit comments