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 1 commit
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
Prev Previous commit
Next Next commit
Adds implementation of rule.
  • Loading branch information
ShreyasJejurkar committed Dec 31, 2023
commit 7975a9d7fefd3d930fec499e64e74a6d65b7ce03
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
Expand Up @@ -2,12 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.

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

namespace Microsoft.DotNet.ApiCompatibility.Rules
{
public class CannotReOrderStructPublicProperties : IRule
public class CannotReOrderStructPublicFields : IRule
{
public CannotReOrderStructPublicProperties(IRuleRegistrationContext context)
public CannotReOrderStructPublicFields(IRuleRegistrationContext context)
{
context.RegisterOnTypeSymbolAction(RunOnTypeSymbol);
}
Expand All @@ -20,17 +21,17 @@ private void RunOnTypeSymbol(ITypeSymbol? left, ITypeSymbol? right, MetadataInfo
// check if left and right both are public
if (left.DeclaredAccessibility == Accessibility.Public && right.DeclaredAccessibility == Accessibility.Public)
{
var leftProperties = left.GetMembers().Where(m => m.Kind == SymbolKind.Property && m.DeclaredAccessibility == Accessibility.Public).ToList();
var rightProperties = right.GetMembers().Where(m => m.Kind == SymbolKind.Property && m.DeclaredAccessibility == Accessibility.Public).ToList();
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 (leftProperties.Count == rightProperties.Count)
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 < leftProperties.Count; i++)
for (int i = 0; i < leftFields.Count; i++)
{
if (leftProperties[i].Name != rightProperties[i].Name)
if (leftFields[i].Name != rightFields[i].Name)
{
sameOrder = false;
break;
Expand All @@ -39,9 +40,12 @@ private void RunOnTypeSymbol(ITypeSymbol? left, ITypeSymbol? right, MetadataInfo

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