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

Handle partial properties in struct field ordering warning #74706

Merged
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 @@ -2734,7 +2734,7 @@ private void CheckSequentialOnPartialType(BindingDiagnosticBag diagnostics)

foreach (var m in syntax.Members)
{
if (HasInstanceData(m))
if (hasInstanceData(m))
{
if (whereFoundField != null && whereFoundField != syntaxRef)
{
Expand All @@ -2754,35 +2754,36 @@ private void CheckSequentialOnPartialType(BindingDiagnosticBag diagnostics)
diagnostics.Add(ErrorCode.WRN_SequentialOnPartialClass, GetFirstLocation(), this);
return;
}
}

private static bool HasInstanceData(MemberDeclarationSyntax m)
{
switch (m.Kind())
{
case SyntaxKind.FieldDeclaration:
var fieldDecl = (FieldDeclarationSyntax)m;
return
!ContainsModifier(fieldDecl.Modifiers, SyntaxKind.StaticKeyword) &&
!ContainsModifier(fieldDecl.Modifiers, SyntaxKind.ConstKeyword);
case SyntaxKind.PropertyDeclaration:
// auto-property
var propertyDecl = (PropertyDeclarationSyntax)m;
return
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.StaticKeyword) &&
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.AbstractKeyword) &&
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.ExternKeyword) &&
propertyDecl.AccessorList != null &&
All(propertyDecl.AccessorList.Accessors, a => a.Body == null && a.ExpressionBody == null);
case SyntaxKind.EventFieldDeclaration:
// field-like event declaration
var eventFieldDecl = (EventFieldDeclarationSyntax)m;
return
!ContainsModifier(eventFieldDecl.Modifiers, SyntaxKind.StaticKeyword) &&
!ContainsModifier(eventFieldDecl.Modifiers, SyntaxKind.AbstractKeyword) &&
!ContainsModifier(eventFieldDecl.Modifiers, SyntaxKind.ExternKeyword);
default:
return false;
static bool hasInstanceData(MemberDeclarationSyntax m)
{
switch (m.Kind())
{
case SyntaxKind.FieldDeclaration:
var fieldDecl = (FieldDeclarationSyntax)m;
return
!ContainsModifier(fieldDecl.Modifiers, SyntaxKind.StaticKeyword) &&
!ContainsModifier(fieldDecl.Modifiers, SyntaxKind.ConstKeyword);
case SyntaxKind.PropertyDeclaration:
// auto-property
var propertyDecl = (PropertyDeclarationSyntax)m;
return
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.StaticKeyword) &&
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.AbstractKeyword) &&
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.ExternKeyword) &&
!ContainsModifier(propertyDecl.Modifiers, SyntaxKind.PartialKeyword) &&
propertyDecl.AccessorList != null &&
All(propertyDecl.AccessorList.Accessors, a => a.Body == null && a.ExpressionBody == null);
case SyntaxKind.EventFieldDeclaration:
// field-like event declaration
var eventFieldDecl = (EventFieldDeclarationSyntax)m;
return
!ContainsModifier(eventFieldDecl.Modifiers, SyntaxKind.StaticKeyword) &&
!ContainsModifier(eventFieldDecl.Modifiers, SyntaxKind.AbstractKeyword) &&
!ContainsModifier(eventFieldDecl.Modifiers, SyntaxKind.ExternKeyword);
default:
return false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4985,7 +4985,7 @@ partial class C
{
public partial int Prop1 { get; set; }
public partial int Prop1 { get => 1; set; }

public partial int Prop2 { get; set; }
public partial int Prop2 { get; set { } }
}
Expand All @@ -5000,5 +5000,51 @@ partial class C
// public partial int Prop2 { get; set { } }
Diagnostic(ErrorCode.ERR_ConcreteMissingBody, "get").WithArguments("C.Prop2.get").WithLocation(7, 32));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74679")]
public void WRN_SequentialOnPartialClass_NotReportedForPartialProperty_01()
{
var source = """
partial struct S
{
partial int I { get; }
}

partial struct S
{
public S() => i = 42;
private readonly int i;
partial int I => i;
}
""";

var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74679")]
public void WRN_SequentialOnPartialClass_NotReportedForPartialProperty_02()
{
var source = """
partial struct S
{
partial int I { get; }
}

partial struct S
{
partial int I => i;
}

partial struct S
{
public S() => i = 42;
private readonly int i;
}
""";

var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics();
}
}
}