From 81a7ed865890da54cbc6f6ac147d60eaeb7c4179 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Tue, 16 Mar 2021 10:34:25 +0100 Subject: [PATCH] Tweaks to ValueComparer nullability Following #24261 --- src/EFCore/ChangeTracking/ValueComparer.cs | 2 ++ src/EFCore/ChangeTracking/ValueComparer`.cs | 34 +++++++++++---------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/EFCore/ChangeTracking/ValueComparer.cs b/src/EFCore/ChangeTracking/ValueComparer.cs index 269f9675b2c..db5115422a5 100644 --- a/src/EFCore/ChangeTracking/ValueComparer.cs +++ b/src/EFCore/ChangeTracking/ValueComparer.cs @@ -9,6 +9,7 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Utilities; +using CA = System.Diagnostics.CodeAnalysis; namespace Microsoft.EntityFrameworkCore.ChangeTracking { @@ -103,6 +104,7 @@ protected ValueComparer( /// /// The instance. /// The snapshot. + [return: CA.NotNullIfNotNull("instance")] public abstract object? Snapshot([CanBeNull] object? instance); /// diff --git a/src/EFCore/ChangeTracking/ValueComparer`.cs b/src/EFCore/ChangeTracking/ValueComparer`.cs index edb9b304c9f..bbbb146fd4c 100644 --- a/src/EFCore/ChangeTracking/ValueComparer`.cs +++ b/src/EFCore/ChangeTracking/ValueComparer`.cs @@ -9,6 +9,7 @@ using System.Reflection; using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Internal; +using CA = System.Diagnostics.CodeAnalysis; namespace Microsoft.EntityFrameworkCore.ChangeTracking { @@ -31,7 +32,7 @@ public class ValueComparer : ValueComparer, IEqualityComparer { private Func? _equals; private Func? _hashCode; - private Func? _snapshot; + private Func? _snapshot; /// /// Creates a new with a default comparison @@ -80,7 +81,7 @@ public ValueComparer( public ValueComparer( [NotNull] Expression> equalsExpression, [NotNull] Expression> hashCodeExpression, - [NotNull] Expression> snapshotExpression) + [NotNull] Expression> snapshotExpression) : base(equalsExpression, hashCodeExpression, snapshotExpression) { } @@ -159,7 +160,7 @@ public ValueComparer( /// Creates an expression for creating a snapshot of a value. /// /// The snapshot expression. - protected static Expression> CreateDefaultSnapshotExpression(bool favorStructuralComparisons) + protected static Expression> CreateDefaultSnapshotExpression(bool favorStructuralComparisons) { if (!favorStructuralComparisons || !typeof(T).IsArray) @@ -176,7 +177,7 @@ public ValueComparer( // var destination = new T[length]; // Array.Copy(source, destination, length); // return destination; - return Expression.Lambda>( + return Expression.Lambda>( Expression.Block( new[] { lengthVariable, destinationVariable }, Expression.Assign( @@ -243,20 +244,21 @@ var expression /// The second instance. /// if they are equal; otherwise. public override bool Equals(object? left, object? right) - { - var v1Null = left == null; - var v2Null = right == null; - - return v1Null || v2Null ? v1Null && v2Null : Equals((T?)left, (T?)right); - } + => (left, right) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + _ => Equals((T)left, (T)right) + }; /// /// Returns the hash code for the given instance. /// /// The instance. /// The hash code. - public override int GetHashCode(object? instance) - => instance == null ? 0 : GetHashCode((T)instance); + public override int GetHashCode(object instance) + => instance is null ? 0 : GetHashCode((T)instance); /// /// Compares the two instances to determine if they are equal. @@ -291,7 +293,7 @@ public virtual int GetHashCode(T instance) /// The instance. /// The snapshot. public override object? Snapshot(object? instance) - => instance == null ? null : (object?)Snapshot((T?)instance); + => instance == null ? null : (object?)Snapshot((T)instance); /// /// @@ -306,7 +308,7 @@ public virtual int GetHashCode(T instance) /// /// The instance. /// The snapshot. - public virtual T? Snapshot([CanBeNull] T? instance) + public virtual T Snapshot([NotNull] T instance) => NonCapturingLazyInitializer.EnsureInitialized( ref _snapshot, this, static c => c.SnapshotExpression.Compile())(instance); @@ -339,7 +341,7 @@ public override Type Type /// reference. /// /// - public new virtual Expression> SnapshotExpression - => (Expression>)base.SnapshotExpression; + public new virtual Expression> SnapshotExpression + => (Expression>)base.SnapshotExpression; } }