From ec76acd3a6dda3a6a0240b45c1b0d109c793003d Mon Sep 17 00:00:00 2001 From: JesterHodl <103882255+jesterhodl@users.noreply.github.com> Date: Sun, 20 Nov 2022 09:42:36 +0100 Subject: [PATCH] Code analysis (#4293) * Enable NETAnalyzers for whole project - remove obsolete analyzers so that the .NET Core SDK NETAnalyzers can be used - enable NETAnalyzers for all projects so that developers can use them by defining the AnalysisMode on individual projects This is because if we set AnalysisMode to minimal, recommended or all it would spam with warning. The idea is to be able to turn them on during development to fix recommended stuff without polluting the build output. Following commits will implement some of the Code Analysis findings * Performance hints for using char overloads for single characters (CA1834 and CA1847) CA1834: Use StringBuilder.Append(char) for single character strings CA1847: Use string.Contains(char) instead of string.Contains(string) with single characters --- BTCPayServer.Common/Logging/ConsoleLogger.cs | 2 +- .../Providers/KrakenExchangeRateProvider.cs | 2 +- BTCPayServer/BTCPayServer.csproj | 9 --------- BTCPayServer/Filters/ContentSecurityPolicyAttribute.cs | 2 +- BTCPayServer/Plugins/Shopify/Models/ShopifySettings.cs | 2 +- BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs | 2 +- Build/Common.csproj | 2 ++ 7 files changed, 7 insertions(+), 14 deletions(-) diff --git a/BTCPayServer.Common/Logging/ConsoleLogger.cs b/BTCPayServer.Common/Logging/ConsoleLogger.cs index def1a79f4..ece125968 100644 --- a/BTCPayServer.Common/Logging/ConsoleLogger.cs +++ b/BTCPayServer.Common/Logging/ConsoleLogger.cs @@ -180,7 +180,7 @@ namespace BTCPayServer.Logging logBuilder.Append(": "); var lenAfter = logBuilder.ToString().Length; while (lenAfter++ < 18) - logBuilder.Append(" "); + logBuilder.Append(' '); // scope information GetScopeInformation(logBuilder); diff --git a/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs b/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs index 3ca425eae..d4be22beb 100644 --- a/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs +++ b/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs @@ -168,7 +168,7 @@ namespace BTCPayServer.Services.Rates sb.Append(url); if (payload != null) { - sb.Append("?"); + sb.Append('?'); sb.Append(String.Join('&', payload.Select(kv => $"{kv.Key}={kv.Value}").OfType().ToArray())); } var request = new HttpRequestMessage(HttpMethod.Get, sb.ToString()); diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj index d4b07b3a4..f3bd39239 100644 --- a/BTCPayServer/BTCPayServer.csproj +++ b/BTCPayServer/BTCPayServer.csproj @@ -54,11 +54,6 @@ - - - all - runtime; build; native; contentfiles; analyzers - @@ -70,10 +65,6 @@ - - all - runtime; build; native; contentfiles; analyzers - diff --git a/BTCPayServer/Filters/ContentSecurityPolicyAttribute.cs b/BTCPayServer/Filters/ContentSecurityPolicyAttribute.cs index 7f303ca5d..b2973b003 100644 --- a/BTCPayServer/Filters/ContentSecurityPolicyAttribute.cs +++ b/BTCPayServer/Filters/ContentSecurityPolicyAttribute.cs @@ -105,7 +105,7 @@ namespace BTCPayServer.Filters { hasSelf = group.Any(g => g.Value.Contains("'self'", StringComparison.OrdinalIgnoreCase)); if (!hasSelf && !group.Any(g => g.Value.Contains("'none'", StringComparison.OrdinalIgnoreCase) || - g.Value.Contains("*", StringComparison.OrdinalIgnoreCase))) + g.Value.Contains('*', StringComparison.OrdinalIgnoreCase))) { policies.Add(new ConsentSecurityPolicy(group.Key, "'self'")); } diff --git a/BTCPayServer/Plugins/Shopify/Models/ShopifySettings.cs b/BTCPayServer/Plugins/Shopify/Models/ShopifySettings.cs index beb41a5ba..23767496f 100644 --- a/BTCPayServer/Plugins/Shopify/Models/ShopifySettings.cs +++ b/BTCPayServer/Plugins/Shopify/Models/ShopifySettings.cs @@ -29,7 +29,7 @@ namespace BTCPayServer.Plugins.Shopify.Models { get { - return ShopName?.Contains(".", StringComparison.OrdinalIgnoreCase) is true ? ShopName : $"https://{ShopName}.myshopify.com"; + return ShopName?.Contains('.', StringComparison.OrdinalIgnoreCase) is true ? ShopName : $"https://{ShopName}.myshopify.com"; } } } diff --git a/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs b/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs index 6e7b8d53c..7a600b428 100644 --- a/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs +++ b/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs @@ -38,7 +38,7 @@ namespace BTCPayServer.Plugins.Shopify string relativeUrl = null) { var url = - $"https://{(shopName.Contains(".", StringComparison.InvariantCulture) ? shopName : $"{shopName}.myshopify.com")}/{relativeUrl ?? ("admin/api/2020-07/" + action)}"; + $"https://{(shopName.Contains('.', StringComparison.InvariantCulture) ? shopName : $"{shopName}.myshopify.com")}/{relativeUrl ?? ("admin/api/2020-07/" + action)}"; var req = new HttpRequestMessage(method, url); return req; } diff --git a/Build/Common.csproj b/Build/Common.csproj index c8427b603..5eff5c831 100644 --- a/Build/Common.csproj +++ b/Build/Common.csproj @@ -4,6 +4,8 @@ $(TargetFrameworkOverride) NU1701,CA1816,CA1308,CA1810,CA2208,CA1303,CA2000,CA2016,CA1835,CA2249,CA9998,CA1704 10.0 + True + 6.0