From 431e72a4c1515064d5952fa61353a5d4a597aa18 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 1 Apr 2023 14:19:28 +0100 Subject: [PATCH] fix: use ArgumentNullException throw helper for .NET >=6 Some of these directives were incorrectly written as #if NET6_0, when ThrowIfNull is available in all future versions too. The macro has been fixed to #if NET6_0_OR_GREATER. For other methods, the branch has been introduced where it didn't exist before. --- X10D/src/Collections/CollectionExtensions.cs | 8 ++++++ X10D/src/Collections/EnumerableExtensions.cs | 28 ++++++++++++++++---- X10D/src/Text/StringBuilderReader.cs | 4 +++ X10D/src/Time/StringExtensions.cs | 4 +++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/X10D/src/Collections/CollectionExtensions.cs b/X10D/src/Collections/CollectionExtensions.cs index ae33488..152e7ba 100644 --- a/X10D/src/Collections/CollectionExtensions.cs +++ b/X10D/src/Collections/CollectionExtensions.cs @@ -16,10 +16,14 @@ public static class CollectionExtensions /// public static void ClearAndDisposeAll(this ICollection source) where T : IDisposable { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); +#else if (source is null) { throw new ArgumentNullException(nameof(source)); } +#endif if (source.IsReadOnly) { @@ -51,10 +55,14 @@ public static class CollectionExtensions /// public static async Task ClearAndDisposeAllAsync(this ICollection source) where T : IAsyncDisposable { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); +#else if (source is null) { throw new ArgumentNullException(nameof(source)); } +#endif if (source.IsReadOnly) { diff --git a/X10D/src/Collections/EnumerableExtensions.cs b/X10D/src/Collections/EnumerableExtensions.cs index 4a2580a..78707e1 100644 --- a/X10D/src/Collections/EnumerableExtensions.cs +++ b/X10D/src/Collections/EnumerableExtensions.cs @@ -24,7 +24,7 @@ public static class EnumerableExtensions [Pure] public static int CountWhereNot(this IEnumerable source, Func predicate) { -#if NET6_0 +#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(predicate); #else @@ -58,7 +58,7 @@ public static class EnumerableExtensions [Pure] public static TSource FirstWhereNot(this IEnumerable source, Func predicate) { -#if NET6_0 +#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(predicate); #else @@ -91,7 +91,7 @@ public static class EnumerableExtensions [Pure] public static TSource? FirstWhereNotOrDefault(this IEnumerable source, Func predicate) { -#if NET6_0 +#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(predicate); #else @@ -127,6 +127,10 @@ public static class EnumerableExtensions /// public static void For(this IEnumerable source, Action action) { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(action); +#else if (source is null) { throw new ArgumentNullException(nameof(source)); @@ -136,6 +140,7 @@ public static class EnumerableExtensions { throw new ArgumentNullException(nameof(action)); } +#endif var index = 0; foreach (T item in source) @@ -161,6 +166,10 @@ public static class EnumerableExtensions /// public static void ForEach(this IEnumerable source, Action action) { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(action); +#else if (source is null) { throw new ArgumentNullException(nameof(source)); @@ -170,6 +179,7 @@ public static class EnumerableExtensions { throw new ArgumentNullException(nameof(action)); } +#endif foreach (T item in source) { @@ -186,10 +196,14 @@ public static class EnumerableExtensions /// public static void DisposeAll(this IEnumerable source) where T : IDisposable { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); +#else if (source is null) { throw new ArgumentNullException(nameof(source)); } +#endif foreach (T item in source) { @@ -213,10 +227,14 @@ public static class EnumerableExtensions /// public static async Task DisposeAllAsync(this IEnumerable source) where T : IAsyncDisposable { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); +#else if (source is null) { throw new ArgumentNullException(nameof(source)); } +#endif foreach (T item in source) { @@ -246,7 +264,7 @@ public static class EnumerableExtensions [Pure] public static TSource LastWhereNot(this IEnumerable source, Func predicate) { -#if NET6_0 +#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(predicate); #else @@ -279,7 +297,7 @@ public static class EnumerableExtensions [Pure] public static TSource? LastWhereNotOrDefault(this IEnumerable source, Func predicate) { -#if NET6_0 +#if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); ArgumentNullException.ThrowIfNull(predicate); #else diff --git a/X10D/src/Text/StringBuilderReader.cs b/X10D/src/Text/StringBuilderReader.cs index fe6e969..337a22c 100644 --- a/X10D/src/Text/StringBuilderReader.cs +++ b/X10D/src/Text/StringBuilderReader.cs @@ -55,10 +55,14 @@ public class StringBuilderReader : TextReader /// public override int Read(char[] buffer, int index, int count) { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(buffer); +#else if (buffer is null) { throw new ArgumentNullException(nameof(buffer)); } +#endif if (index < 0) { diff --git a/X10D/src/Time/StringExtensions.cs b/X10D/src/Time/StringExtensions.cs index 0772ff5..dbb154b 100644 --- a/X10D/src/Time/StringExtensions.cs +++ b/X10D/src/Time/StringExtensions.cs @@ -64,10 +64,14 @@ public static class StringExtensions #endif public static TimeSpan ToTimeSpan(this string input) { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(input); +#else if (input is null) { throw new ArgumentNullException(nameof(input)); } +#endif return TimeSpanParser.TryParse(input, out TimeSpan result) ? result