From 559fdb34c6fddcce5e9a64cae50aea36a6652002 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Thu, 6 May 2021 23:32:50 +0200 Subject: [PATCH] Resolve CA1062 --- .editorconfig | 1 - .../Collections/ConcurrentHashSet.cs | 8 +++ ArchiSteamFarm/RuntimeCompatibility/Path.cs | 8 +++ .../RuntimeCompatibility/StaticHelpers.cs | 49 +++++++++++++++++-- ArchiSteamFarm/Utilities.cs | 16 +++++- 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/.editorconfig b/.editorconfig index 6219895eb..a862120ab 100644 --- a/.editorconfig +++ b/.editorconfig @@ -111,7 +111,6 @@ dotnet_diagnostic.ca1028.severity = silent dotnet_diagnostic.ca1031.severity = silent # TODO - one at a time -dotnet_diagnostic.ca1062.severity = silent dotnet_diagnostic.ca1063.severity = silent dotnet_diagnostic.ca1303.severity = silent dotnet_diagnostic.ca1307.severity = silent diff --git a/ArchiSteamFarm/Collections/ConcurrentHashSet.cs b/ArchiSteamFarm/Collections/ConcurrentHashSet.cs index c9cd831e6..0d51287f3 100644 --- a/ArchiSteamFarm/Collections/ConcurrentHashSet.cs +++ b/ArchiSteamFarm/Collections/ConcurrentHashSet.cs @@ -70,6 +70,10 @@ namespace ArchiSteamFarm.Collections { public void CopyTo(T[] array, int arrayIndex) => BackingCollection.Keys.CopyTo(array, arrayIndex); public void ExceptWith(IEnumerable other) { + if (other == null) { + throw new ArgumentNullException(nameof(other)); + } + foreach (T item in other) { Remove(item); } @@ -146,6 +150,10 @@ namespace ArchiSteamFarm.Collections { } public void UnionWith(IEnumerable other) { + if (other == null) { + throw new ArgumentNullException(nameof(other)); + } + foreach (T otherElement in other) { Add(otherElement); } diff --git a/ArchiSteamFarm/RuntimeCompatibility/Path.cs b/ArchiSteamFarm/RuntimeCompatibility/Path.cs index 3b7bf63c7..55bb862e9 100644 --- a/ArchiSteamFarm/RuntimeCompatibility/Path.cs +++ b/ArchiSteamFarm/RuntimeCompatibility/Path.cs @@ -29,6 +29,14 @@ namespace ArchiSteamFarm.RuntimeCompatibility { public static class Path { public static string GetRelativePath(string relativeTo, string path) { #if NETFRAMEWORK + if (relativeTo == null) { + throw new ArgumentNullException(nameof(relativeTo)); + } + + if (path == null) { + throw new ArgumentNullException(nameof(path)); + } + if (!path.StartsWith(relativeTo, StringComparison.Ordinal)) { throw new NotImplementedException(); } diff --git a/ArchiSteamFarm/RuntimeCompatibility/StaticHelpers.cs b/ArchiSteamFarm/RuntimeCompatibility/StaticHelpers.cs index 98c48050e..c6f20b98e 100644 --- a/ArchiSteamFarm/RuntimeCompatibility/StaticHelpers.cs +++ b/ArchiSteamFarm/RuntimeCompatibility/StaticHelpers.cs @@ -60,15 +60,31 @@ namespace ArchiSteamFarm.RuntimeCompatibility { } #if NETFRAMEWORK - public static Task ComputeHashAsync(this HashAlgorithm hashAlgorithm, Stream inputStream) => Task.FromResult(hashAlgorithm.ComputeHash(inputStream)); + public static Task ComputeHashAsync(this HashAlgorithm hashAlgorithm, Stream inputStream) { + if (hashAlgorithm == null) { + throw new ArgumentNullException(nameof(hashAlgorithm)); + } + + return Task.FromResult(hashAlgorithm.ComputeHash(inputStream)); + } public static IWebHostBuilder ConfigureWebHostDefaults(this IWebHostBuilder builder, Action configure) { + if (configure == null) { + throw new ArgumentNullException(nameof(configure)); + } + configure(builder); return builder; } - public static bool Contains(this string input, string value, StringComparison comparisonType) => input.IndexOf(value, comparisonType) >= 0; + public static bool Contains(this string input, string value, StringComparison comparisonType) { + if (input == null) { + throw new ArgumentNullException(nameof(input)); + } + + return input.IndexOf(value, comparisonType) >= 0; + } // ReSharper disable once UseDeconstructionOnParameter - we actually implement deconstruction here public static void Deconstruct(this KeyValuePair kv, out TKey key, out TValue value) { @@ -77,15 +93,38 @@ namespace ArchiSteamFarm.RuntimeCompatibility { } public static ValueTask DisposeAsync(this IDisposable disposable) { + if (disposable == null) { + throw new ArgumentNullException(nameof(disposable)); + } + disposable.Dispose(); return default(ValueTask); } - public static async Task ReceiveAsync(this WebSocket webSocket, byte[] buffer, CancellationToken cancellationToken) => await webSocket.ReceiveAsync(new ArraySegment(buffer), cancellationToken).ConfigureAwait(false); - public static async Task SendAsync(this WebSocket webSocket, byte[] buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) => await webSocket.SendAsync(new ArraySegment(buffer), messageType, endOfMessage, cancellationToken).ConfigureAwait(false); + public static async Task ReceiveAsync(this WebSocket webSocket, byte[] buffer, CancellationToken cancellationToken) { + if (webSocket == null) { + throw new ArgumentNullException(nameof(webSocket)); + } - public static string[] Split(this string text, char separator, StringSplitOptions options = StringSplitOptions.None) => text.Split(new[] { separator }, options); + return await webSocket.ReceiveAsync(new ArraySegment(buffer), cancellationToken).ConfigureAwait(false); + } + + public static async Task SendAsync(this WebSocket webSocket, byte[] buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) { + if (webSocket == null) { + throw new ArgumentNullException(nameof(webSocket)); + } + + await webSocket.SendAsync(new ArraySegment(buffer), messageType, endOfMessage, cancellationToken).ConfigureAwait(false); + } + + public static string[] Split(this string text, char separator, StringSplitOptions options = StringSplitOptions.None) { + if (text == null) { + throw new ArgumentNullException(nameof(text)); + } + + return text.Split(new[] { separator }, options); + } public static void TrimExcess(this Dictionary _) { } // no-op #endif diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index bc5c448b6..ff0618585 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -235,13 +235,25 @@ namespace ArchiSteamFarm { public static IEnumerable SelectElementNodes(this IElement element, string xpath) => element.SelectNodes(xpath).OfType(); [PublicAPI] - public static IEnumerable SelectNodes(this IDocument document, string xpath) => document.Body.SelectNodes(xpath).OfType(); + public static IEnumerable SelectNodes(this IDocument document, string xpath) { + if (document == null) { + throw new ArgumentNullException(nameof(document)); + } + + return document.Body.SelectNodes(xpath).OfType(); + } [PublicAPI] public static IElement? SelectSingleElementNode(this IElement element, string xpath) => (IElement?) element.SelectSingleNode(xpath); [PublicAPI] - public static IElement? SelectSingleNode(this IDocument document, string xpath) => (IElement?) document.Body.SelectSingleNode(xpath); + public static IElement? SelectSingleNode(this IDocument document, string xpath) { + if (document == null) { + throw new ArgumentNullException(nameof(document)); + } + + return (IElement?) document.Body.SelectSingleNode(xpath); + } [PublicAPI] public static IEnumerable ToEnumerable(this T item) {