Skip to content

Commit 61f19b3

Browse files
[RGen] Follow the BCL docs recomendation and inherit from the abstract class. (#21722)
1 parent 5e89d63 commit 61f19b3

18 files changed

+67
-51
lines changed

src/rgen/Microsoft.Macios.Generator/DataModel/Accessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public bool Equals (Accessor other)
4444
var attrsComparer = new AttributesEqualityComparer ();
4545
if (!attrsComparer.Equals (Attributes, other.Attributes))
4646
return false;
47-
var modifiersComparer = new ModifiersComparer ();
47+
var modifiersComparer = new ModifiersEqualityComparer ();
4848
return modifiersComparer.Equals (Modifiers, other.Modifiers);
4949
}
5050

src/rgen/Microsoft.Macios.Generator/DataModel/AccessorsEqualityComparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class AccessorsEqualityComparer : IEqualityComparer<ImmutableArray<Accessor>> {
9-
public bool Equals (ImmutableArray<Accessor> x, ImmutableArray<Accessor> y)
8+
class AccessorsEqualityComparer : EqualityComparer<ImmutableArray<Accessor>> {
9+
public override bool Equals (ImmutableArray<Accessor> x, ImmutableArray<Accessor> y)
1010
{
1111
// property accessor kinds cannot be duplicated due to the definition of the language, create two dictionaries
1212
// using the kind as a key and then re-use our dictionary comparer
@@ -16,7 +16,7 @@ public bool Equals (ImmutableArray<Accessor> x, ImmutableArray<Accessor> y)
1616
return comparer.Equals (xDictionary, yDictionary);
1717
}
1818

19-
public int GetHashCode (ImmutableArray<Accessor> obj)
19+
public override int GetHashCode (ImmutableArray<Accessor> obj)
2020
{
2121
var hashCode = new HashCode ();
2222
foreach (var accessor in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/AttributesEqualityComparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public int Compare (AttributeCodeChange x, AttributeCodeChange y)
3030
return 0;
3131
}
3232
}
33-
class AttributesEqualityComparer : IEqualityComparer<ImmutableArray<AttributeCodeChange>> {
33+
class AttributesEqualityComparer : EqualityComparer<ImmutableArray<AttributeCodeChange>> {
3434

35-
public bool Equals (ImmutableArray<AttributeCodeChange> x, ImmutableArray<AttributeCodeChange> y)
35+
public override bool Equals (ImmutableArray<AttributeCodeChange> x, ImmutableArray<AttributeCodeChange> y)
3636
{
3737
if (x.Length != y.Length)
3838
return false;
@@ -46,7 +46,7 @@ public bool Equals (ImmutableArray<AttributeCodeChange> x, ImmutableArray<Attrib
4646
return true;
4747
}
4848

49-
public int GetHashCode (ImmutableArray<AttributeCodeChange> obj)
49+
public override int GetHashCode (ImmutableArray<AttributeCodeChange> obj)
5050
{
5151
var hash = new HashCode ();
5252
foreach (var change in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/CodeChangesEqualityComparer.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44

55
namespace Microsoft.Macios.Generator.DataModel;
66

7-
class DeclarationCodeChangesEqualityComparer : IEqualityComparer<(BaseTypeDeclarationSyntax Declaration, CodeChanges
7+
class DeclarationCodeChangesEqualityComparer : EqualityComparer<(BaseTypeDeclarationSyntax Declaration, CodeChanges
88
Changes)> {
99
readonly CodeChangesEqualityComparer comparer = new ();
1010

11-
public bool Equals ((BaseTypeDeclarationSyntax Declaration, CodeChanges Changes) x,
11+
/// <inheritdoc/>
12+
public override bool Equals ((BaseTypeDeclarationSyntax Declaration, CodeChanges Changes) x,
1213
(BaseTypeDeclarationSyntax Declaration, CodeChanges Changes) y)
1314
{
1415
return comparer.Equals (x.Changes, y.Changes);
1516
}
1617

17-
public int GetHashCode ((BaseTypeDeclarationSyntax Declaration, CodeChanges Changes) obj)
18+
/// <inheritdoc/>
19+
public override int GetHashCode ((BaseTypeDeclarationSyntax Declaration, CodeChanges Changes) obj)
1820
=> comparer.GetHashCode (obj.Changes);
1921
}
2022

2123
/// <summary>
2224
/// Custom code changes comparer used for the Roslyn code generation to invalidate caching.
2325
/// </summary>
24-
class CodeChangesEqualityComparer : IEqualityComparer<CodeChanges> {
26+
class CodeChangesEqualityComparer : EqualityComparer<CodeChanges> {
2527
/// <inheritdoc />
26-
public bool Equals (CodeChanges x, CodeChanges y)
28+
public override bool Equals (CodeChanges x, CodeChanges y)
2729
{
2830
// things that mean a code change is the same:
2931
// - the fully qualified symbol is the same
@@ -74,7 +76,7 @@ public bool Equals (CodeChanges x, CodeChanges y)
7476
}
7577

7678
/// <inheritdoc />
77-
public int GetHashCode (CodeChanges obj)
79+
public override int GetHashCode (CodeChanges obj)
7880
{
7981
return HashCode.Combine (obj.FullyQualifiedSymbol, obj.EnumMembers);
8082
}

src/rgen/Microsoft.Macios.Generator/DataModel/Constructor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public bool Equals (Constructor other)
6868
var attrsComparer = new AttributesEqualityComparer ();
6969
if (!attrsComparer.Equals (Attributes, other.Attributes))
7070
return false;
71-
var modifiersComparer = new ModifiersComparer ();
71+
var modifiersComparer = new ModifiersEqualityComparer ();
7272
if (!modifiersComparer.Equals (Modifiers, other.Modifiers))
7373
return false;
7474

src/rgen/Microsoft.Macios.Generator/DataModel/ConstructorsEqualityComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class ConstructorsEqualityComparer : IEqualityComparer<ImmutableArray<Constructor>> {
9-
public bool Equals (ImmutableArray<Constructor> x, ImmutableArray<Constructor> y)
8+
class ConstructorsEqualityComparer : EqualityComparer<ImmutableArray<Constructor>> {
9+
/// <inheritdoc/>
10+
public override bool Equals (ImmutableArray<Constructor> x, ImmutableArray<Constructor> y)
1011
{
1112
// group the constructors based on the number of parameters. We create two dictionaries, that will have
1213
// the number of params as the key, and a list of constructors as the value
@@ -19,7 +20,8 @@ public bool Equals (ImmutableArray<Constructor> x, ImmutableArray<Constructor> y
1920
return dictionaryComparer.Equals (xConstructors, yConstructors);
2021
}
2122

22-
public int GetHashCode (ImmutableArray<Constructor> obj)
23+
/// <inheritdoc/>
24+
public override int GetHashCode (ImmutableArray<Constructor> obj)
2325
{
2426
var hashCode = new HashCode ();
2527
foreach (var ctr in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/EnumMembersEqualityComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class EnumMembersEqualityComparer : IEqualityComparer<ImmutableArray<EnumMember>> {
8+
class EnumMembersEqualityComparer : EqualityComparer<ImmutableArray<EnumMember>> {
99

10-
public bool Equals (ImmutableArray<EnumMember> x, ImmutableArray<EnumMember> y)
10+
/// <inheritdoc/>
11+
public override bool Equals (ImmutableArray<EnumMember> x, ImmutableArray<EnumMember> y)
1112
{
1213
if (x.Length != y.Length)
1314
return false;
@@ -20,7 +21,8 @@ public bool Equals (ImmutableArray<EnumMember> x, ImmutableArray<EnumMember> y)
2021
return true;
2122
}
2223

23-
public int GetHashCode (ImmutableArray<EnumMember> obj)
24+
/// <inheritdoc/>
25+
public override int GetHashCode (ImmutableArray<EnumMember> obj)
2426
{
2527
var hash = new HashCode ();
2628
foreach (var change in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/Event.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public bool Equals (Event other)
5858
if (!attrsComparer.Equals (Attributes, other.Attributes))
5959
return false;
6060

61-
var modifiersComparer = new ModifiersComparer ();
61+
var modifiersComparer = new ModifiersEqualityComparer ();
6262
if (!modifiersComparer.Equals (Modifiers, other.Modifiers))
6363
return false;
6464

src/rgen/Microsoft.Macios.Generator/DataModel/EventEqualityComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class EventEqualityComparer : IEqualityComparer<ImmutableArray<Event>> {
8+
class EventEqualityComparer : EqualityComparer<ImmutableArray<Event>> {
99

10-
public bool Equals (ImmutableArray<Event> x, ImmutableArray<Event> y)
10+
/// <inheritdoc/>
11+
public override bool Equals (ImmutableArray<Event> x, ImmutableArray<Event> y)
1112
{
1213
// properties are unique by their name, that means that we can build two dicts and comparethem
1314
var xDictionary = Enumerable.ToDictionary (x, property => property.Name);
@@ -17,7 +18,8 @@ public bool Equals (ImmutableArray<Event> x, ImmutableArray<Event> y)
1718
return comparer.Equals (xDictionary, yDictionary);
1819
}
1920

20-
public int GetHashCode (ImmutableArray<Event> obj)
21+
/// <inheritdoc/>
22+
public override int GetHashCode (ImmutableArray<Event> obj)
2123
{
2224
var hash = new HashCode ();
2325
foreach (var property in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/Method.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public bool Equals (Method other)
8080
var attrsComparer = new AttributesEqualityComparer ();
8181
if (!attrsComparer.Equals (Attributes, other.Attributes))
8282
return false;
83-
var modifiersComparer = new ModifiersComparer ();
83+
var modifiersComparer = new ModifiersEqualityComparer ();
8484
if (!modifiersComparer.Equals (Modifiers, other.Modifiers))
8585
return false;
8686

src/rgen/Microsoft.Macios.Generator/DataModel/MethodsEqualityComparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class MethodsEqualityComparer : IEqualityComparer<ImmutableArray<Method>> {
8+
class MethodsEqualityComparer : EqualityComparer<ImmutableArray<Method>> {
99
/// <inheritdoc/>
10-
public bool Equals (ImmutableArray<Method> x, ImmutableArray<Method> y)
10+
public override bool Equals (ImmutableArray<Method> x, ImmutableArray<Method> y)
1111
{
1212
// to compare two lists, we need to do the following:
1313
// 1. Group all the methods by return type + name + param count, this way we
@@ -27,7 +27,7 @@ public bool Equals (ImmutableArray<Method> x, ImmutableArray<Method> y)
2727
}
2828

2929
/// <inheritdoc/>
30-
public int GetHashCode (ImmutableArray<Method> obj)
30+
public override int GetHashCode (ImmutableArray<Method> obj)
3131
{
3232
var hashCode = new HashCode ();
3333
foreach (var ctr in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/ModifiersComparer.cs renamed to src/rgen/Microsoft.Macios.Generator/DataModel/ModifiersEqualityComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace Microsoft.Macios.Generator.DataModel;
88

9-
public class ModifiersComparer : IEqualityComparer<ImmutableArray<SyntaxToken>> {
10-
public bool Equals (ImmutableArray<SyntaxToken> x, ImmutableArray<SyntaxToken> y)
9+
public class ModifiersEqualityComparer : EqualityComparer<ImmutableArray<SyntaxToken>> {
10+
/// <inheritdoc/>
11+
public override bool Equals (ImmutableArray<SyntaxToken> x, ImmutableArray<SyntaxToken> y)
1112
{
1213
if (x.Length != y.Length)
1314
return false;
@@ -21,7 +22,8 @@ public bool Equals (ImmutableArray<SyntaxToken> x, ImmutableArray<SyntaxToken> y
2122
return true;
2223
}
2324

24-
public int GetHashCode (ImmutableArray<SyntaxToken> obj)
25+
/// <inheritdoc/>
26+
public override int GetHashCode (ImmutableArray<SyntaxToken> obj)
2527
{
2628
var hash = new HashCode ();
2729
foreach (var token in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/ParameterEqualityComparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class ParameterEqualityComparer : IEqualityComparer<ImmutableArray<Parameter>> {
8+
class ParameterEqualityComparer : EqualityComparer<ImmutableArray<Parameter>> {
99

1010
/// <inheritdoc/>
11-
public bool Equals (ImmutableArray<Parameter> x, ImmutableArray<Parameter> y)
11+
public override bool Equals (ImmutableArray<Parameter> x, ImmutableArray<Parameter> y)
1212
{
1313
// we know as a fact that parameter names have to be unique, otherwise we could not
1414
// compile the code. So make sure that all the parameters with the same name are the same
@@ -20,7 +20,7 @@ public bool Equals (ImmutableArray<Parameter> x, ImmutableArray<Parameter> y)
2020
}
2121

2222
/// <inheritdoc/>
23-
public int GetHashCode (ImmutableArray<Parameter> obj)
23+
public override int GetHashCode (ImmutableArray<Parameter> obj)
2424
{
2525
var hashCode = new HashCode ();
2626
foreach (var constructor in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/PropertiesEqualityComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
namespace Microsoft.Macios.Generator.DataModel;
77

8-
class PropertiesEqualityComparer : IEqualityComparer<ImmutableArray<Property>> {
9-
public bool Equals (ImmutableArray<Property> x, ImmutableArray<Property> y)
8+
class PropertiesEqualityComparer : EqualityComparer<ImmutableArray<Property>> {
9+
/// <inheritdoc/>
10+
public override bool Equals (ImmutableArray<Property> x, ImmutableArray<Property> y)
1011
{
1112
// properties are unique by their name, that means that we can build two dicts and comparethem
1213
var xDictionary = Enumerable.ToDictionary (x, property => property.Name);
@@ -16,7 +17,8 @@ public bool Equals (ImmutableArray<Property> x, ImmutableArray<Property> y)
1617
return comparer.Equals (xDictionary, yDictionary);
1718
}
1819

19-
public int GetHashCode (ImmutableArray<Property> obj)
20+
/// <inheritdoc/>
21+
public override int GetHashCode (ImmutableArray<Property> obj)
2022
{
2123
var hash = new HashCode ();
2224
foreach (var property in obj) {

src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public bool Equals (Property other)
6060
if (!attrsComparer.Equals (Attributes, other.Attributes))
6161
return false;
6262

63-
var modifiersComparer = new ModifiersComparer ();
63+
var modifiersComparer = new ModifiersEqualityComparer ();
6464
if (!modifiersComparer.Equals (Modifiers, other.Modifiers))
6565
return false;
6666

src/rgen/Microsoft.Macios.Generator/DictionaryComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace Microsoft.Macios.Generator;
66

77
public class DictionaryComparer<TKey, TValue> (IEqualityComparer<TValue>? valueComparer = null)
8-
: IEqualityComparer<IDictionary<TKey, TValue>>
8+
: EqualityComparer<IDictionary<TKey, TValue>>
99
where TKey : notnull {
1010
readonly IEqualityComparer<TValue> valueComparer = valueComparer ?? EqualityComparer<TValue>.Default;
1111

12-
public bool Equals (IDictionary<TKey, TValue>? x, IDictionary<TKey, TValue>? y)
12+
/// <inheritdoc/>
13+
public override bool Equals (IDictionary<TKey, TValue>? x, IDictionary<TKey, TValue>? y)
1314
{
1415
if (x is null && y is null)
1516
return true;
@@ -24,7 +25,8 @@ public bool Equals (IDictionary<TKey, TValue>? x, IDictionary<TKey, TValue>? y)
2425
return x.All (pair => valueComparer.Equals (pair.Value, y [pair.Key]));
2526
}
2627

27-
public int GetHashCode (IDictionary<TKey, TValue> obj)
28+
/// <inheritdoc/>
29+
public override int GetHashCode (IDictionary<TKey, TValue> obj)
2830
{
2931
var hash = new HashCode ();
3032
foreach (var (key, value) in obj) {

src/rgen/Microsoft.Macios.Generator/ListComparer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Microsoft.Macios.Generator;
55

6-
public class ListComparer<T> : IEqualityComparer<List<T>> {
6+
public class ListComparer<T> : EqualityComparer<List<T>> {
77
readonly IComparer<T> comparer;
88
readonly IEqualityComparer<T> valueComparer;
99

@@ -13,7 +13,8 @@ public ListComparer (IComparer<T> sortComparer, IEqualityComparer<T>? equalityCo
1313
valueComparer = equalityComparer ?? EqualityComparer<T>.Default;
1414
}
1515

16-
public bool Equals (List<T>? x, List<T>? y)
16+
/// <inheritdoc/>
17+
public override bool Equals (List<T>? x, List<T>? y)
1718
{
1819
// bases cases for null or diff size
1920
if (x is null && y is null)
@@ -37,7 +38,8 @@ public bool Equals (List<T>? x, List<T>? y)
3738
return true;
3839
}
3940

40-
public int GetHashCode (List<T> obj)
41+
/// <inheritdoc/>
42+
public override int GetHashCode (List<T> obj)
4143
{
4244
var hash = new HashCode ();
4345
foreach (var element in obj) {

tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/ModifiersComparerTests.cs renamed to tests/rgen/Microsoft.Macios.Generator.Tests/DataModel/ModifiersEqualityComparerTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
namespace Microsoft.Macios.Generator.Tests.DataModel;
88

9-
public class ModifiersComparerTests {
10-
readonly ModifiersComparer comparer = new ModifiersComparer ();
9+
public class ModifiersEqualityComparerTests {
10+
readonly ModifiersEqualityComparer equalityComparer = new ModifiersEqualityComparer ();
1111

1212
[Fact]
1313
public void CompareEmpty ()
1414
{
1515
var x = ImmutableArray<SyntaxToken>.Empty;
1616
var y = ImmutableArray<SyntaxToken>.Empty;
17-
Assert.True (comparer.Equals (x, y));
17+
Assert.True (equalityComparer.Equals (x, y));
1818
}
1919

2020
[Fact]
@@ -27,7 +27,7 @@ public void CompareDifferentSize ()
2727
ImmutableArray<SyntaxToken> y = [
2828
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
2929
];
30-
Assert.False (comparer.Equals (x, y));
30+
Assert.False (equalityComparer.Equals (x, y));
3131
}
3232

3333
[Fact]
@@ -41,7 +41,7 @@ public void CompareDifferentModifiers ()
4141
SyntaxFactory.Token (SyntaxKind.ProtectedKeyword),
4242
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
4343
];
44-
Assert.False (comparer.Equals (x, y));
44+
Assert.False (equalityComparer.Equals (x, y));
4545
}
4646

4747
[Fact]
@@ -56,7 +56,7 @@ public void CompareSameModifiersDiffOrder ()
5656
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
5757
];
5858

59-
Assert.True (comparer.Equals (x, y));
59+
Assert.True (equalityComparer.Equals (x, y));
6060
}
6161

6262
[Fact]
@@ -71,6 +71,6 @@ public void CompareSameModifiers ()
7171
SyntaxFactory.Token (SyntaxKind.PrivateKeyword),
7272
];
7373

74-
Assert.True (comparer.Equals (x, y));
74+
Assert.True (equalityComparer.Equals (x, y));
7575
}
7676
}

0 commit comments

Comments
 (0)