Skip to content

Commit f9f13c3

Browse files
committed
Cleanup: use pattern matching
1 parent 4b28eae commit f9f13c3

File tree

8 files changed

+15
-19
lines changed

8 files changed

+15
-19
lines changed

Main/src/Algorithms/HashCode.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ public static int CombineValues<T>([CanBeNull, InstantHandle] IEnumerable<T> val
144144

145145
var hashCode = 0;
146146

147-
var list = values as IList<T>;
148-
if (list != null)
147+
if (values is IList<T> list)
149148
{
150149
for (int i = 0, count = list.Count; i < count; i++)
151150
{

Main/src/Collections/Enumerable/EnumerableExtensions.Slice.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ public static IEnumerable<T> Slice<T>([NotNull] this IEnumerable<T> source, int
3434
if (count <= 0)
3535
return Enumerable.Empty<T>();
3636

37-
var list = source as IList<T>;
38-
if (list == null)
39-
return SliceImpl(source, startIndex, count);
40-
37+
if (source is IList<T> list)
4138
return startIndex != 0 || list.Count > count
4239
? SliceImpl(list, startIndex, count)
4340
: list;
41+
42+
return SliceImpl(source, startIndex, count);
4443
}
4544

4645
private static IEnumerable<T> SliceImpl<T>(IList<T> list, int index, int count)

Main/src/Collections/Enumerable/EnumerableExtensions.TakeLast.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ public static IEnumerable<T> TakeLast<T>([NotNull] this IEnumerable<T> source, i
2828
if (count <= 0)
2929
return Enumerable.Empty<T>();
3030

31-
var list = source as IList<T>;
32-
if (list == null)
33-
return TakeLastImpl(source, count);
34-
31+
if (source is IList<T> list)
3532
return count < list.Count
3633
? TakeLastImpl(list, count)
3734
: list;
35+
36+
return TakeLastImpl(source, count);
3837
}
3938

4039
private static IEnumerable<T> TakeLastImpl<T>(IList<T> source, int count)

Main/src/Collections/Enumerable/IndexedItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public IndexedItem(T item, int index, bool isFirst, bool isLast) : this()
7474
public override bool Equals(object obj)
7575
{
7676
if (ReferenceEquals(null, obj)) return false;
77+
// ReSharper disable once MergeCastWithTypeCheck
7778
return obj is IndexedItem<T> && Equals((IndexedItem<T>)obj);
7879
}
7980

Main/src/ExceptionExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public static StringBuilder ToDiagnosticString(this Exception exception, [NotNul
4343
if (ex.StackTrace.NotNullNorEmpty())
4444
stringBuilder.AppendLine(ex.StackTrace);
4545

46-
var notFoundException = ex as FileNotFoundException;
47-
48-
if (notFoundException != null)
46+
if (ex is FileNotFoundException notFoundException)
4947
{
5048
var fex = notFoundException;
5149

Main/src/Expressions/ExpressionExtensions.GetMembers.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public static MemberInfo GetMemberInfo([NotNull] this LambdaExpression expressio
2929
Code.NotNull(expression, nameof(expression));
3030

3131
var body = expression.Body;
32-
var unary = body as UnaryExpression;
33-
if (unary != null)
32+
if (body is UnaryExpression unary)
3433
body = unary.Operand;
3534

3635
switch (body.NodeType)
@@ -140,8 +139,7 @@ private static string GetFullPropertyNameImpl(MemberExpression expression)
140139
private static MemberExpression GetMemberExpression(LambdaExpression expression)
141140
{
142141
var body = expression.Body;
143-
var unary = body as UnaryExpression;
144-
return unary != null
142+
return body is UnaryExpression unary
145143
? (MemberExpression)unary.Operand
146144
: (MemberExpression)body;
147145
}
@@ -159,8 +157,7 @@ public static MemberInfo[] GetMembersInfo([NotNull] this LambdaExpression expres
159157
Code.NotNull(expression, nameof(expression));
160158

161159
var body = expression.Body;
162-
var unary = body as UnaryExpression;
163-
if (unary != null)
160+
if (body is UnaryExpression unary)
164161
body = unary.Operand;
165162

166163
return GetMembers(body).Reverse().ToArray();

Main/src/Ranges/[Boundaries]/RangeBoundaryFrom`1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,13 @@ public int CompareTo(T other) =>
517517
[Pure]
518518
int IComparable.CompareTo(object obj)
519519
{
520+
// ReSharper disable once UsePatternMatching
520521
var otherA = obj as RangeBoundaryFrom<T>?;
521522
if (otherA != null)
522523
{
523524
return CompareTo(otherA.GetValueOrDefault());
524525
}
526+
// ReSharper disable once UsePatternMatching
525527
var otherB = obj as RangeBoundaryTo<T>?;
526528
if (otherB != null)
527529
{

Main/src/Structures/Option/ValueOption`1.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public bool Equals(ValueOption<T> other) =>
8888
public override bool Equals(object obj)
8989
{
9090
if (ReferenceEquals(null, obj)) return false;
91+
// ReSharper disable once MergeCastWithTypeCheck
9192
return obj is ValueOption<T> && Equals((ValueOption<T>)obj);
9293
}
9394

0 commit comments

Comments
 (0)