Skip to content

Commit c4e023f

Browse files
authored
Updated appropriate null checks to all use pattern matching (#550)
1 parent b0d3d9a commit c4e023f

File tree

9 files changed

+25
-25
lines changed

9 files changed

+25
-25
lines changed

src/SmartEnum.Dapper/SmartEnumByNameTypeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public SmartEnumByNameTypeHandler()
3737
/// <inheritdoc/>
3838
public override TEnum Parse(object value)
3939
{
40-
if (value == null || value is DBNull)
40+
if (value is null || value is DBNull)
4141
return null;
4242

4343
if (value is string stringValue)

src/SmartEnum.Dapper/SmartEnumByValueTypeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public SmartEnumByValueTypeHandler()
3434
/// <inheritdoc/>
3535
public override TEnum Parse(object value)
3636
{
37-
if (value == null || value is DBNull)
37+
if (value is null || value is DBNull)
3838
return null;
3939

4040
if (value is not TValue tValue)

src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void ConfigureSmartEnum(this ModelConfigurationBuilder configurati
2727
var propertyTypes = modelBuilder.Model.GetEntityTypes()
2828
.SelectMany(e => e.ClrType.GetProperties())
2929
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)))
30-
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() == null)
30+
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() is null)
3131
.Select(p => p.PropertyType)
3232
.Distinct();
3333

@@ -58,7 +58,7 @@ public static void ConfigureSmartEnum(this ModelBuilder modelBuilder)
5858
{
5959
var properties = entityType.ClrType.GetProperties()
6060
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)))
61-
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() == null);
61+
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() is null);
6262

6363
foreach (var property in properties)
6464
{
@@ -74,7 +74,7 @@ public static void ConfigureSmartEnum(this ModelBuilder modelBuilder)
7474
var converter = (ValueConverter)Activator.CreateInstance(converterType);
7575

7676
var propertyBuilder = GetPropertyBuilder(modelBuilder, entityType, property.Name);
77-
if (propertyBuilder == null)
77+
if (propertyBuilder is null)
7878
{
7979
continue;
8080
}
@@ -111,7 +111,7 @@ private static PropertyBuilder GetPropertyBuilder(
111111
}
112112

113113
var ownedNavigationBuilder = GetOwnedNavigationBuilder(entityTypeBuilder, ownershipPath);
114-
if (ownedNavigationBuilder == null)
114+
if (ownedNavigationBuilder is null)
115115
{
116116
return null;
117117
}
@@ -129,12 +129,12 @@ private static OwnedNavigationBuilder GetOwnedNavigationBuilder(
129129
var ownership = ownershipPath[i];
130130

131131
var navigation = ownership.GetNavigation(pointsToPrincipal: false);
132-
if (navigation == null)
132+
if (navigation is null)
133133
{
134134
return null;
135135
}
136136

137-
if (ownedNavigationBuilder == null)
137+
if (ownedNavigationBuilder is null)
138138
{
139139
if (ownership.IsUnique)
140140
{

src/SmartEnum.ModelBinding/SmartEnumBinderProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class SmartEnumBinderProvider : IModelBinderProvider
88
{
99
public IModelBinder GetBinder(ModelBinderProviderContext context)
1010
{
11-
if (context == null)
11+
if (context is null)
1212
throw new ArgumentNullException(nameof(context));
1313

1414
if (TypeUtil.IsDerived(context.Metadata.ModelType, typeof(SmartEnum<,>)))

src/SmartEnum/SmartEnum.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public abstract class SmartEnum<TEnum, TValue> :
6161
var dictionary = new Dictionary<TValue, TEnum>(GetValueComparer());
6262
foreach (var item in _enumOptions.Value)
6363
{
64-
if (item._value != null && !dictionary.ContainsKey(item._value))
64+
if (item._value is not null && !dictionary.ContainsKey(item._value))
6565
dictionary.Add(item._value, item);
6666
}
6767
return dictionary;
@@ -217,7 +217,7 @@ public static TEnum FromValue(TValue value)
217217
{
218218
TEnum result;
219219

220-
if (value != null)
220+
if (value is not null)
221221
{
222222
if (!_fromValue.Value.TryGetValue(value, out result))
223223
{
@@ -226,7 +226,7 @@ public static TEnum FromValue(TValue value)
226226
}
227227
else
228228
{
229-
result = _enumOptions.Value.FirstOrDefault(x => x.Value == null);
229+
result = _enumOptions.Value.FirstOrDefault(x => x.Value is null);
230230
if (result == null)
231231
{
232232
ThrowHelper.ThrowValueNotFoundException<TEnum, TValue>(value);
@@ -248,7 +248,7 @@ public static TEnum FromValue(TValue value)
248248
/// <seealso cref="SmartEnum{TEnum, TValue}.TryFromValue(TValue, out TEnum)"/>
249249
public static TEnum FromValue(TValue value, TEnum defaultValue)
250250
{
251-
if (value == null)
251+
if (value is null)
252252
ThrowHelper.ThrowArgumentNullException(nameof(value));
253253

254254
if (!_fromValue.Value.TryGetValue(value, out var result))
@@ -272,7 +272,7 @@ public static TEnum FromValue(TValue value, TEnum defaultValue)
272272
/// <seealso cref="SmartEnum{TEnum, TValue}.FromValue(TValue, TEnum)"/>
273273
public static bool TryFromValue(TValue value, out TEnum result)
274274
{
275-
if (value == null)
275+
if (value is null)
276276
{
277277
result = default;
278278
return false;

src/SmartEnum/SmartEnumExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static bool IsSmartEnum(this Type type, out Type[] genericArguments)
4242

4343
type = type.BaseType;
4444
}
45-
while (!(type is null));
45+
while (type is not null);
4646

4747
genericArguments = null;
4848
return false;

src/SmartEnum/SmartFlagEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected static IEnumerable<TEnum> GetFlagEnumValues(TValue value, IEnumerable<
6969

7070
private static void GuardAgainstNull(TValue value)
7171
{
72-
if (value == null)
72+
if (value is null)
7373
ThrowHelper.ThrowArgumentNullException(nameof(value));
7474
}
7575

@@ -84,7 +84,7 @@ private static void GuardAgainstNegativeInputValue(TValue value)
8484
AllowNegativeInputValuesAttribute attribute = (AllowNegativeInputValuesAttribute)
8585
Attribute.GetCustomAttribute(typeof(TEnum), typeof(AllowNegativeInputValuesAttribute));
8686

87-
if (attribute == null && int.Parse(value.ToString()) < -1)
87+
if (attribute is null && int.Parse(value.ToString()) < -1)
8888
{
8989
ThrowHelper.ThrowNegativeValueArgumentException<TEnum, TValue>(value);
9090
}
@@ -130,7 +130,7 @@ private static void ApplyUnsafeFlagEnumAttributeSettings(IEnumerable<TEnum> list
130130
AllowUnsafeFlagEnumValuesAttribute attribute = (AllowUnsafeFlagEnumValuesAttribute)
131131
Attribute.GetCustomAttribute(typeof(TEnum), typeof(AllowUnsafeFlagEnumValuesAttribute));
132132

133-
if (attribute == null)
133+
if (attribute is null)
134134
{
135135
CheckEnumListForPowersOfTwo(list);
136136
}

src/SmartEnum/SmartFlagEnum.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected SmartFlagEnum(string name, TValue value)
100100
{
101101
if (String.IsNullOrEmpty(name))
102102
ThrowHelper.ThrowArgumentNullOrEmptyException(nameof(name));
103-
if (value == null)
103+
if (value is null)
104104
ThrowHelper.ThrowArgumentNullException(nameof(value));
105105

106106
_name = name;
@@ -187,10 +187,10 @@ public static bool TryFromName(string names, bool ignoreCase, out IEnumerable<TE
187187
[SuppressMessage("Minor Code Smell", "S4136:Method overloads should be grouped together", Justification = "<Pending>")]
188188
public static IEnumerable<TEnum> FromValue(TValue value)
189189
{
190-
if (value == null)
190+
if (value is null)
191191
ThrowHelper.ThrowArgumentNullException(nameof(value));
192192

193-
if (GetFlagEnumValues(value, GetAllOptions()) == null)
193+
if (GetFlagEnumValues(value, GetAllOptions()) is null)
194194
{
195195
ThrowHelper.ThrowValueNotFoundException<TEnum, TValue>(value);
196196
}
@@ -231,7 +231,7 @@ public static TEnum DeserializeValue(TValue value)
231231
/// <seealso cref="SmartFlagEnum{TEnum, TValue}.FromValue(TValue)"/>
232232
public static IEnumerable<TEnum> FromValue(TValue value, IEnumerable<TEnum> defaultValue)
233233
{
234-
if (value == null)
234+
if (value is null)
235235
ThrowHelper.ThrowArgumentNullException(nameof(value));
236236

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

259259

260260
result = GetFlagEnumValues(value, GetAllOptions());
261-
if (result == null)
261+
if (result is null)
262262
{
263263
return false;
264264
}

src/SmartEnum/SmartFlagEnumExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static bool IsSmartFlagEnum(this Type type, out Type[] genericArguments)
4242

4343
type = type.BaseType;
4444
}
45-
while (!(type is null));
45+
while (type is not null);
4646

4747
genericArguments = null;
4848
return false;

0 commit comments

Comments
 (0)