Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates appropriate null checks to all use pattern matching #550

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SmartEnumByNameTypeHandler()
/// <inheritdoc/>
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)
Expand Down
2 changes: 1 addition & 1 deletion src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SmartEnumByValueTypeHandler()
/// <inheritdoc/>
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)
Expand Down
12 changes: 6 additions & 6 deletions src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotMappedAttribute>() == null)
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() is null)
.Select(p => p.PropertyType)
.Distinct();

Expand Down Expand Up @@ -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<NotMappedAttribute>() == null);
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() is null);

foreach (var property in properties)
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ private static PropertyBuilder GetPropertyBuilder(
}

var ownedNavigationBuilder = GetOwnedNavigationBuilder(entityTypeBuilder, ownershipPath);
if (ownedNavigationBuilder == null)
if (ownedNavigationBuilder is null)
{
return null;
}
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SmartEnum.ModelBinding/SmartEnumBinderProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<,>)))
Expand Down
10 changes: 5 additions & 5 deletions src/SmartEnum/SmartEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public abstract class SmartEnum<TEnum, TValue> :
var dictionary = new Dictionary<TValue, TEnum>(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;
Expand Down Expand Up @@ -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))
{
Expand All @@ -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<TEnum, TValue>(value);
Expand All @@ -248,7 +248,7 @@ public static TEnum FromValue(TValue value)
/// <seealso cref="SmartEnum{TEnum, TValue}.TryFromValue(TValue, out TEnum)"/>
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))
Expand All @@ -272,7 +272,7 @@ public static TEnum FromValue(TValue value, TEnum defaultValue)
/// <seealso cref="SmartEnum{TEnum, TValue}.FromValue(TValue, TEnum)"/>
public static bool TryFromValue(TValue value, out TEnum result)
{
if (value == null)
if (value is null)
{
result = default;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/SmartEnum/SmartEnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/SmartEnum/SmartFlagEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected static IEnumerable<TEnum> GetFlagEnumValues(TValue value, IEnumerable<

private static void GuardAgainstNull(TValue value)
{
if (value == null)
if (value is null)
ThrowHelper.ThrowArgumentNullException(nameof(value));
}

Expand All @@ -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<TEnum, TValue>(value);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ private static void ApplyUnsafeFlagEnumAttributeSettings(IEnumerable<TEnum> list
AllowUnsafeFlagEnumValuesAttribute attribute = (AllowUnsafeFlagEnumValuesAttribute)
Attribute.GetCustomAttribute(typeof(TEnum), typeof(AllowUnsafeFlagEnumValuesAttribute));

if (attribute == null)
if (attribute is null)
{
CheckEnumListForPowersOfTwo(list);
}
Expand Down
12 changes: 6 additions & 6 deletions src/SmartEnum/SmartFlagEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -187,10 +187,10 @@ public static bool TryFromName(string names, bool ignoreCase, out IEnumerable<TE
[SuppressMessage("Minor Code Smell", "S4136:Method overloads should be grouped together", Justification = "<Pending>")]
public static IEnumerable<TEnum> 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<TEnum, TValue>(value);
}
Expand Down Expand Up @@ -231,7 +231,7 @@ public static TEnum DeserializeValue(TValue value)
/// <seealso cref="SmartFlagEnum{TEnum, TValue}.FromValue(TValue)"/>
public static IEnumerable<TEnum> FromValue(TValue value, IEnumerable<TEnum> defaultValue)
{
if (value == null)
if (value is null)
ThrowHelper.ThrowArgumentNullException(nameof(value));

return !TryFromValue(value, out var result) ? defaultValue : result;
Expand All @@ -250,15 +250,15 @@ public static IEnumerable<TEnum> FromValue(TValue value, IEnumerable<TEnum> defa
/// <seealso cref="SmartFlagEnum{TEnum, TValue}.FromValue(TValue)"/>
public static bool TryFromValue(TValue value, out IEnumerable<TEnum> result)
{
if (value == null || !int.TryParse(value.ToString(), out _))
if (value is null || !int.TryParse(value.ToString(), out _))
{
result = default;
return false;
}


result = GetFlagEnumValues(value, GetAllOptions());
if (result == null)
if (result is null)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartEnum/SmartFlagEnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down