From 1afe2e466ceb1038295185275113efcaabe33c4d Mon Sep 17 00:00:00 2001 From: ArmoryNode <22787155+ArmoryNode@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:20:34 -0700 Subject: [PATCH] Updated appropriate null checks to all use pattern matching --- src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs | 2 +- src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs | 2 +- src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs | 12 ++++++------ .../SmartEnumBinderProvider.cs | 2 +- src/SmartEnum/SmartEnum.cs | 10 +++++----- src/SmartEnum/SmartEnumExtensions.cs | 2 +- src/SmartEnum/SmartFlagEngine.cs | 6 +++--- src/SmartEnum/SmartFlagEnum.cs | 12 ++++++------ src/SmartEnum/SmartFlagEnumExtensions.cs | 2 +- 9 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs b/src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs index 61c8daf9..a5bcbce9 100644 --- a/src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs +++ b/src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs @@ -37,7 +37,7 @@ public SmartEnumByNameTypeHandler() /// public override TEnum Parse(object value) { - if (value == null || value is DBNull) + if (value is null || value is DBNull) return null; if (value is string stringValue) diff --git a/src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs b/src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs index 944333ca..5856b4e5 100644 --- a/src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs +++ b/src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs @@ -34,7 +34,7 @@ public SmartEnumByValueTypeHandler() /// public override TEnum Parse(object value) { - if (value == null || value is DBNull) + if (value is null || value is DBNull) return null; if (value is not TValue tValue) diff --git a/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs b/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs index c3ad9955..bfd36f8b 100644 --- a/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs +++ b/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs @@ -27,7 +27,7 @@ public static void ConfigureSmartEnum(this ModelConfigurationBuilder configurati var propertyTypes = modelBuilder.Model.GetEntityTypes() .SelectMany(e => e.ClrType.GetProperties()) .Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>))) - .Where(p => p.GetCustomAttribute() == null) + .Where(p => p.GetCustomAttribute() is null) .Select(p => p.PropertyType) .Distinct(); @@ -58,7 +58,7 @@ public static void ConfigureSmartEnum(this ModelBuilder modelBuilder) { var properties = entityType.ClrType.GetProperties() .Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>))) - .Where(p => p.GetCustomAttribute() == null); + .Where(p => p.GetCustomAttribute() is null); foreach (var property in properties) { @@ -74,7 +74,7 @@ public static void ConfigureSmartEnum(this ModelBuilder modelBuilder) var converter = (ValueConverter)Activator.CreateInstance(converterType); var propertyBuilder = GetPropertyBuilder(modelBuilder, entityType, property.Name); - if (propertyBuilder == null) + if (propertyBuilder is null) { continue; } @@ -111,7 +111,7 @@ private static PropertyBuilder GetPropertyBuilder( } var ownedNavigationBuilder = GetOwnedNavigationBuilder(entityTypeBuilder, ownershipPath); - if (ownedNavigationBuilder == null) + if (ownedNavigationBuilder is null) { return null; } @@ -129,12 +129,12 @@ private static OwnedNavigationBuilder GetOwnedNavigationBuilder( var ownership = ownershipPath[i]; var navigation = ownership.GetNavigation(pointsToPrincipal: false); - if (navigation == null) + if (navigation is null) { return null; } - if (ownedNavigationBuilder == null) + if (ownedNavigationBuilder is null) { if (ownership.IsUnique) { diff --git a/src/SmartEnum.ModelBinding/SmartEnumBinderProvider.cs b/src/SmartEnum.ModelBinding/SmartEnumBinderProvider.cs index bf8fc909..34cedf5c 100644 --- a/src/SmartEnum.ModelBinding/SmartEnumBinderProvider.cs +++ b/src/SmartEnum.ModelBinding/SmartEnumBinderProvider.cs @@ -8,7 +8,7 @@ public class SmartEnumBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) { - if (context == null) + if (context is null) throw new ArgumentNullException(nameof(context)); if (TypeUtil.IsDerived(context.Metadata.ModelType, typeof(SmartEnum<,>))) diff --git a/src/SmartEnum/SmartEnum.cs b/src/SmartEnum/SmartEnum.cs index 00b424d5..7c7b84db 100644 --- a/src/SmartEnum/SmartEnum.cs +++ b/src/SmartEnum/SmartEnum.cs @@ -61,7 +61,7 @@ public abstract class SmartEnum : var dictionary = new Dictionary(GetValueComparer()); foreach (var item in _enumOptions.Value) { - if (item._value != null && !dictionary.ContainsKey(item._value)) + if (item._value is not null && !dictionary.ContainsKey(item._value)) dictionary.Add(item._value, item); } return dictionary; @@ -217,7 +217,7 @@ public static TEnum FromValue(TValue value) { TEnum result; - if (value != null) + if (value is not null) { if (!_fromValue.Value.TryGetValue(value, out result)) { @@ -226,7 +226,7 @@ public static TEnum FromValue(TValue value) } else { - result = _enumOptions.Value.FirstOrDefault(x => x.Value == null); + result = _enumOptions.Value.FirstOrDefault(x => x.Value is null); if (result == null) { ThrowHelper.ThrowValueNotFoundException(value); @@ -248,7 +248,7 @@ public static TEnum FromValue(TValue value) /// public static TEnum FromValue(TValue value, TEnum defaultValue) { - if (value == null) + if (value is null) ThrowHelper.ThrowArgumentNullException(nameof(value)); if (!_fromValue.Value.TryGetValue(value, out var result)) @@ -272,7 +272,7 @@ public static TEnum FromValue(TValue value, TEnum defaultValue) /// public static bool TryFromValue(TValue value, out TEnum result) { - if (value == null) + if (value is null) { result = default; return false; diff --git a/src/SmartEnum/SmartEnumExtensions.cs b/src/SmartEnum/SmartEnumExtensions.cs index e590c34a..56ea1fa3 100644 --- a/src/SmartEnum/SmartEnumExtensions.cs +++ b/src/SmartEnum/SmartEnumExtensions.cs @@ -42,7 +42,7 @@ public static bool IsSmartEnum(this Type type, out Type[] genericArguments) type = type.BaseType; } - while (!(type is null)); + while (type is not null); genericArguments = null; return false; diff --git a/src/SmartEnum/SmartFlagEngine.cs b/src/SmartEnum/SmartFlagEngine.cs index 8ac50832..c6f2ad70 100644 --- a/src/SmartEnum/SmartFlagEngine.cs +++ b/src/SmartEnum/SmartFlagEngine.cs @@ -69,7 +69,7 @@ protected static IEnumerable GetFlagEnumValues(TValue value, IEnumerable< private static void GuardAgainstNull(TValue value) { - if (value == null) + if (value is null) ThrowHelper.ThrowArgumentNullException(nameof(value)); } @@ -84,7 +84,7 @@ private static void GuardAgainstNegativeInputValue(TValue value) AllowNegativeInputValuesAttribute attribute = (AllowNegativeInputValuesAttribute) Attribute.GetCustomAttribute(typeof(TEnum), typeof(AllowNegativeInputValuesAttribute)); - if (attribute == null && int.Parse(value.ToString()) < -1) + if (attribute is null && int.Parse(value.ToString()) < -1) { ThrowHelper.ThrowNegativeValueArgumentException(value); } @@ -130,7 +130,7 @@ private static void ApplyUnsafeFlagEnumAttributeSettings(IEnumerable list AllowUnsafeFlagEnumValuesAttribute attribute = (AllowUnsafeFlagEnumValuesAttribute) Attribute.GetCustomAttribute(typeof(TEnum), typeof(AllowUnsafeFlagEnumValuesAttribute)); - if (attribute == null) + if (attribute is null) { CheckEnumListForPowersOfTwo(list); } diff --git a/src/SmartEnum/SmartFlagEnum.cs b/src/SmartEnum/SmartFlagEnum.cs index acc3b2e4..d4a01d4f 100644 --- a/src/SmartEnum/SmartFlagEnum.cs +++ b/src/SmartEnum/SmartFlagEnum.cs @@ -100,7 +100,7 @@ protected SmartFlagEnum(string name, TValue value) { if (String.IsNullOrEmpty(name)) ThrowHelper.ThrowArgumentNullOrEmptyException(nameof(name)); - if (value == null) + if (value is null) ThrowHelper.ThrowArgumentNullException(nameof(value)); _name = name; @@ -187,10 +187,10 @@ public static bool TryFromName(string names, bool ignoreCase, out IEnumerable FromValue(TValue value) { - if (value == null) + if (value is null) ThrowHelper.ThrowArgumentNullException(nameof(value)); - if (GetFlagEnumValues(value, GetAllOptions()) == null) + if (GetFlagEnumValues(value, GetAllOptions()) is null) { ThrowHelper.ThrowValueNotFoundException(value); } @@ -231,7 +231,7 @@ public static TEnum DeserializeValue(TValue value) /// public static IEnumerable FromValue(TValue value, IEnumerable defaultValue) { - if (value == null) + if (value is null) ThrowHelper.ThrowArgumentNullException(nameof(value)); return !TryFromValue(value, out var result) ? defaultValue : result; @@ -250,7 +250,7 @@ public static IEnumerable FromValue(TValue value, IEnumerable defa /// public static bool TryFromValue(TValue value, out IEnumerable result) { - if (value == null || !int.TryParse(value.ToString(), out _)) + if (value is null || !int.TryParse(value.ToString(), out _)) { result = default; return false; @@ -258,7 +258,7 @@ public static bool TryFromValue(TValue value, out IEnumerable result) result = GetFlagEnumValues(value, GetAllOptions()); - if (result == null) + if (result is null) { return false; } diff --git a/src/SmartEnum/SmartFlagEnumExtensions.cs b/src/SmartEnum/SmartFlagEnumExtensions.cs index 3989db18..125fa856 100644 --- a/src/SmartEnum/SmartFlagEnumExtensions.cs +++ b/src/SmartEnum/SmartFlagEnumExtensions.cs @@ -42,7 +42,7 @@ public static bool IsSmartFlagEnum(this Type type, out Type[] genericArguments) type = type.BaseType; } - while (!(type is null)); + while (type is not null); genericArguments = null; return false;