From 8c9c2e1be5ffd0223a4e7cfb66adf41e6559a500 Mon Sep 17 00:00:00 2001 From: Nima Ara Date: Sun, 18 Jun 2023 16:23:59 +0100 Subject: [PATCH] Improve Nullability --- Easy.Common/Ensure.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Easy.Common/Ensure.cs b/Easy.Common/Ensure.cs index 4066581..bbf9686 100644 --- a/Easy.Common/Ensure.cs +++ b/Easy.Common/Ensure.cs @@ -1,11 +1,12 @@ namespace Easy.Common; +using Easy.Common.Extensions; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; -using Easy.Common.Extensions; /// /// Helper class that will exceptions when conditions are not satisfied. @@ -22,7 +23,7 @@ public static class Ensure /// Thrown when TException is . /// [DebuggerStepThrough] - public static void That(bool condition, string message = "The given condition is false.") where TException : Exception + public static void That([DoesNotReturnIf(false)] bool condition, string message = "The given condition is false.") where TException : Exception { if (!condition) { throw (TException)Activator.CreateInstance(typeof(TException), message)!; } } @@ -36,7 +37,7 @@ public static void That(bool condition, string message = "The given /// Thrown when is . /// [DebuggerStepThrough] - public static void That(bool condition, string message = "The given condition is false.") => + public static void That([DoesNotReturnIf(false)] bool condition, string message = "The given condition is false.") => That(condition, message); /// @@ -49,7 +50,7 @@ public static void That(bool condition, string message = "The given condition is /// Thrown when is . /// [DebuggerStepThrough] - public static void Not(bool condition, string message = "The given condition is true.") where TException : Exception => + public static void Not([DoesNotReturnIf(true)] bool condition, string message = "The given condition is true.") where TException : Exception => That(!condition, message); /// @@ -61,7 +62,7 @@ public static void Not(bool condition, string message = "The given c /// Thrown when is . /// [DebuggerStepThrough] - public static void Not(bool condition, string message = "The given condition is true.") => + public static void Not([DoesNotReturnIf(true)] bool condition, string message = "The given condition is true.") => Not(condition, message); /// @@ -93,9 +94,9 @@ public static T NotNull(T? value, string paramName) where T : class /// /// Null values will cause an exception to be thrown [DebuggerStepThrough] - public static void Equal(T left, T right, string message = "Values must be equal.") => + public static void Equal(T left, T right, string message = "Values must be equal.") => That(Comparer.Default.Compare(left, right) == 0, message); - + /// /// Ensures given objects are not equal. /// @@ -108,7 +109,7 @@ public static void Equal(T left, T right, string message = "Values must be eq /// /// Null values will cause an exception to be thrown [DebuggerStepThrough] - public static void NotEqual(T left, T right, string message = "Values must not be equal.") => + public static void NotEqual(T left, T right, string message = "Values must not be equal.") => That(Comparer.Default.Compare(left, right) != 0, message); ///