From 2233327096c33a77ca497458e0ae4239cf55883e Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Fri, 27 May 2022 17:52:35 +0900 Subject: [PATCH 1/2] Add IsNull()/IsNotNull() helper extensions --- .../ObjectExtensions/ObjectExtensions.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs b/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs index cc3437c311..2ea9499790 100644 --- a/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs +++ b/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs @@ -1,10 +1,10 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Diagnostics; - #nullable enable +using System.Diagnostics; + namespace osu.Framework.Extensions.ObjectExtensions { /// @@ -27,5 +27,15 @@ public static T AsNonNull(this T? obj) Debug.Assert(obj != null); return obj; } + + /// + /// If the given object is null. + /// + public static bool IsNull(this T obj) => ReferenceEquals(obj, null); + + /// + /// true if the given object is not null, false otherwise. + /// + public static bool IsNotNull(this T obj) => !ReferenceEquals(obj, null); } } From 1f7270482ce3fe7d69389c3de1ba4cd80ed3157f Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Fri, 27 May 2022 18:10:47 +0900 Subject: [PATCH 2/2] Annotate return value for consumers --- .../Extensions/ObjectExtensions/ObjectExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs b/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs index 2ea9499790..b7f698ff3a 100644 --- a/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs +++ b/osu.Framework/Extensions/ObjectExtensions/ObjectExtensions.cs @@ -4,6 +4,7 @@ #nullable enable using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace osu.Framework.Extensions.ObjectExtensions { @@ -31,11 +32,11 @@ public static T AsNonNull(this T? obj) /// /// If the given object is null. /// - public static bool IsNull(this T obj) => ReferenceEquals(obj, null); + public static bool IsNull([NotNullWhen(false)] this T obj) => ReferenceEquals(obj, null); /// /// true if the given object is not null, false otherwise. /// - public static bool IsNotNull(this T obj) => !ReferenceEquals(obj, null); + public static bool IsNotNull([NotNullWhen(true)] this T obj) => !ReferenceEquals(obj, null); } }