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

Add CannotReOrderStructProperties rule to API-Compat #37662

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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 @@ -28,6 +28,7 @@ public static class DiagnosticIds
public const string CannotAddSealedToInterfaceMember = "CP0018";
public const string CannotReduceVisibility = "CP0019";
public const string CannotExpandVisibility = "CP0020";
public const string CannotReOrderStructPublicFields = "CP0021";

// Assembly loading ids
public const string AssemblyReferenceNotFound = "CP1002";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,7 @@
<data name="CannotReduceVisibility" xml:space="preserve">
<value>Visibility of '{0}' reduced from '{1}' to '{2}'.</value>
</data>
<data name="CannotReOrderStructPublicFields" xml:space="preserve">
<value>Struct public fields order have been changed in '{0}'.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.CodeAnalysis;
using Microsoft.DotNet.ApiSymbolExtensions;

namespace Microsoft.DotNet.ApiCompatibility.Rules
{
/// <summary>
/// This rule validates that order of struct fields is maintained.
/// </summary>
public class CannotReOrderStructPublicFields : IRule
{
public CannotReOrderStructPublicFields(IRuleRegistrationContext context)
{
context.RegisterOnTypeSymbolAction(RunOnTypeSymbol);
}

private void RunOnTypeSymbol(ITypeSymbol? left, ITypeSymbol? right, MetadataInformation leftMetadata, MetadataInformation rightMetadata, IList<CompatDifference> differences)
{
// check if left and right both are struct
if (left != null && right != null && left.TypeKind == TypeKind.Struct && right.TypeKind == TypeKind.Struct)
{
// check if left and right both are public
if (left.DeclaredAccessibility == Accessibility.Public && right.DeclaredAccessibility == Accessibility.Public)
{
var leftFields = left.GetMembers().Where(m => m.Kind == SymbolKind.Field && m.DeclaredAccessibility == Accessibility.Public).ToList();
var rightFields = right.GetMembers().Where(m => m.Kind == SymbolKind.Field && m.DeclaredAccessibility == Accessibility.Public).ToList();

// check if left and right both have same number of public properties
if (leftFields.Count == rightFields.Count)
{
// check if left and right both have same public properties in same order
bool sameOrder = true;
for (int i = 0; i < leftFields.Count; i++)
{
if (leftFields[i].Name != rightFields[i].Name)
{
sameOrder = false;
break;
}
}

if (!sameOrder)
{
differences.Add(new CompatDifference(
leftMetadata,
rightMetadata,
DiagnosticIds.CannotReOrderStructPublicFields,
string.Format(Resources.CannotReOrderStructPublicFields, right.ToDisplayString(SymbolExtensions.DisplayFormat)),
DifferenceType.Changed, right));
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public IRule[] CreateRules(IRuleSettings settings, IRuleRegistrationContext cont
new CannotSealType(settings, context),
new EnumsMustMatch(settings, context),
new MembersMustExist(settings, context),
new CannotChangeVisibility(settings, context)
new CannotChangeVisibility(settings, context),
new CannotReOrderStructPublicFields(context)
};

if (enableRuleAttributesMustMatch)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading