Skip to content

Commit 8ee0305

Browse files
authored
Tweaks to ValueComparer nullability (#24410)
* Make Snapshot accept/receive non-nullable (nulls are sanitized externally). * Make ValueComparer<T>.GetHashCode accept non-nullable object.
1 parent 6412d18 commit 8ee0305

File tree

15 files changed

+48
-79
lines changed

15 files changed

+48
-79
lines changed

src/EFCore.Cosmos/ChangeTracking/Internal/ListComparer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,8 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
7777
return hash.ToHashCode();
7878
}
7979

80-
private static TCollection? Snapshot(TCollection? source, ValueComparer<TElement> elementComparer, bool readOnly)
80+
private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
8181
{
82-
if (source is null)
83-
{
84-
return null;
85-
}
86-
8782
if (readOnly)
8883
{
8984
return source;
@@ -92,7 +87,7 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
9287
var snapshot = new List<TElement>(((IReadOnlyList<TElement>)source).Count);
9388
foreach (var e in source)
9489
{
95-
snapshot.Add(elementComparer.Snapshot(e)!);
90+
snapshot.Add(e is null ? default! : elementComparer.Snapshot(e));
9691
}
9792

9893
return (TCollection)(object)snapshot;

src/EFCore.Cosmos/ChangeTracking/Internal/NullableListComparer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,8 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
8989
return hash.ToHashCode();
9090
}
9191

92-
private static TCollection? Snapshot(TCollection? source, ValueComparer<TElement> elementComparer, bool readOnly)
92+
private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
9393
{
94-
if (source is null)
95-
{
96-
return null;
97-
}
98-
9994
if (readOnly)
10095
{
10196
return source;

src/EFCore.Cosmos/ChangeTracking/Internal/NullableSingleDimensionalArrayComparer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,8 @@ private static int GetHashCode(TElement?[] source, ValueComparer<TElement> eleme
8888
}
8989

9090
[return: NotNullIfNotNull("source")]
91-
private static TElement?[]? Snapshot(TElement?[]? source, ValueComparer<TElement> elementComparer)
91+
private static TElement?[] Snapshot(TElement?[] source, ValueComparer<TElement> elementComparer)
9292
{
93-
if (source is null)
94-
{
95-
return null;
96-
}
97-
9893
var snapshot = new TElement?[source.Length];
9994
for (var i = 0; i < source.Length; i++)
10095
{

src/EFCore.Cosmos/ChangeTracking/Internal/NullableStringDictionaryComparer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,8 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
9595
return hash.ToHashCode();
9696
}
9797

98-
private static TCollection? Snapshot(TCollection? source, ValueComparer<TElement> elementComparer, bool readOnly)
98+
private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
9999
{
100-
if (source is null)
101-
{
102-
return null;
103-
}
104-
105100
if (readOnly)
106101
{
107102
return source;

src/EFCore.Cosmos/ChangeTracking/Internal/SingleDimensionalArrayComparer.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,13 @@ private static int GetHashCode(TElement[] source, ValueComparer<TElement> elemen
7676
}
7777

7878
[return: NotNullIfNotNull("source")]
79-
private static TElement[]? Snapshot(TElement[]? source, ValueComparer<TElement> elementComparer)
79+
private static TElement[] Snapshot(TElement[] source, ValueComparer<TElement> elementComparer)
8080
{
81-
if (source is null)
82-
{
83-
return null;
84-
}
85-
8681
var snapshot = new TElement[source.Length];
8782
for (var i = 0; i < source.Length; i++)
8883
{
89-
snapshot[i] = elementComparer.Snapshot(source[i])!;
84+
var element = source[i];
85+
snapshot[i] = element is null ? default! : elementComparer.Snapshot(source[i]);
9086
}
9187
return snapshot;
9288
}

src/EFCore.Cosmos/ChangeTracking/Internal/StringDictionaryComparer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,8 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
7979
return hash.ToHashCode();
8080
}
8181

82-
private static TCollection? Snapshot(TCollection? source, ValueComparer<TElement> elementComparer, bool readOnly)
82+
private static TCollection Snapshot(TCollection source, ValueComparer<TElement> elementComparer, bool readOnly)
8383
{
84-
if (source is null)
85-
{
86-
return null;
87-
}
88-
8984
if (readOnly)
9085
{
9186
return source;
@@ -94,7 +89,7 @@ private static int GetHashCode(TCollection source, ValueComparer<TElement> eleme
9489
var snapshot = new Dictionary<string, TElement>(((IReadOnlyDictionary<string, TElement>)source).Count);
9590
foreach (var e in source)
9691
{
97-
snapshot.Add(e.Key, elementComparer.Snapshot(e.Value)!);
92+
snapshot.Add(e.Key, e.Value is null ? default! : elementComparer.Snapshot(e.Value));
9893
}
9994

10095
return (TCollection)(object)snapshot;

src/EFCore.SqlServer/Storage/Internal/SqlServerTypeMappingSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private readonly SqlServerByteArrayTypeMapping _rowversion
5252
comparer: new ValueComparer<byte[]>(
5353
(v1, v2) => StructuralComparisons.StructuralEqualityComparer.Equals(v1, v2),
5454
v => StructuralComparisons.StructuralEqualityComparer.GetHashCode(v),
55-
v => v == null ? null : v.ToArray()),
55+
v => v.ToArray()),
5656
storeTypePostfix: StoreTypePostfix.None);
5757

5858
private readonly IntTypeMapping _int

src/EFCore/ChangeTracking/ArrayStructuralComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ArrayStructuralComparer()
2424
: base(
2525
CreateDefaultEqualsExpression(),
2626
CreateDefaultHashCodeExpression(favorStructuralComparisons: true),
27-
v => v == null ? null : v.ToArray())
27+
v => v.ToArray())
2828
{
2929
}
3030
}

src/EFCore/ChangeTracking/GeometryValueComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public GeometryValueComparer()
5858
right);
5959
}
6060

61-
private static Expression<Func<TGeometry?, TGeometry?>> GetSnapshotExpression()
61+
private static Expression<Func<TGeometry, TGeometry>> GetSnapshotExpression()
6262
{
6363
var instance = Expression.Parameter(typeof(TGeometry), "instance");
6464

@@ -71,7 +71,7 @@ public GeometryValueComparer()
7171
body = Expression.Convert(body, typeof(TGeometry));
7272
}
7373

74-
return Expression.Lambda<Func<TGeometry?, TGeometry?>>(body, instance);
74+
return Expression.Lambda<Func<TGeometry, TGeometry>>(body, instance);
7575
}
7676
}
7777
}

src/EFCore/ChangeTracking/Internal/SimplePrincipalKeyValueFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public NoNullsCustomEqualityComparer(ValueComparer comparer)
148148
public bool Equals(TKey? x, TKey? y)
149149
=> _equals(x, y);
150150

151-
public int GetHashCode(TKey obj)
151+
public int GetHashCode([DisallowNull] TKey obj)
152152
=> _hashCode(obj);
153153
}
154154
}

0 commit comments

Comments
 (0)