From d667c7d853ce33dbb290ae9119658d1fe53f32fd Mon Sep 17 00:00:00 2001 From: Bogdan Date: Fri, 14 Apr 2023 07:39:20 +0300 Subject: [PATCH] Fixed: Use Array.Empty and fix a few multiple enumerations (cherry picked from commit 11d91faaada0e70910c832ce405ddeed52a24172) --- src/NzbDrone.Common/Composition/AssemblyLoader.cs | 11 +++++------ src/NzbDrone.Common/OAuth/WebParameterCollection.cs | 4 ++-- src/NzbDrone.Core/Datastore/BasicRepository.cs | 2 +- .../Proxies/DownloadStationTaskProxy.cs | 3 ++- src/NzbDrone.Core/IndexerSearch/NewznabResults.cs | 2 +- .../IndexerSearch/ReleaseSearchService.cs | 8 ++++---- .../Indexers/IndexerCapabilitiesCategories.cs | 4 ++-- src/NzbDrone.Core/Indexers/IndexerFactory.cs | 2 +- src/NzbDrone.Core/Indexers/RssParser.cs | 7 ++++++- src/Prowlarr.Api.V1/ProviderControllerBase.cs | 6 +++--- src/Prowlarr.Http/REST/RestController.cs | 4 +++- 11 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/NzbDrone.Common/Composition/AssemblyLoader.cs b/src/NzbDrone.Common/Composition/AssemblyLoader.cs index 82476eee2..0ff69fb0c 100644 --- a/src/NzbDrone.Common/Composition/AssemblyLoader.cs +++ b/src/NzbDrone.Common/Composition/AssemblyLoader.cs @@ -5,8 +5,6 @@ using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Loader; -using System.Text; -using System.Threading.Tasks; using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common.Composition @@ -19,16 +17,17 @@ namespace NzbDrone.Common.Composition RegisterSQLiteResolver(); } - public static IEnumerable Load(IEnumerable assemblies) + public static IList Load(IList assemblyNames) { - var toLoad = assemblies.ToList(); + var toLoad = assemblyNames.ToList(); toLoad.Add("Prowlarr.Common"); toLoad.Add(OsInfo.IsWindows ? "Prowlarr.Windows" : "Prowlarr.Mono"); var startupPath = AppDomain.CurrentDomain.BaseDirectory; - return toLoad.Select(x => - AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{x}.dll"))); + return toLoad + .Select(x => AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{x}.dll"))) + .ToList(); } private static Assembly ContainerResolveEventHandler(object sender, ResolveEventArgs args) diff --git a/src/NzbDrone.Common/OAuth/WebParameterCollection.cs b/src/NzbDrone.Common/OAuth/WebParameterCollection.cs index cea68b114..9dbd90509 100644 --- a/src/NzbDrone.Common/OAuth/WebParameterCollection.cs +++ b/src/NzbDrone.Common/OAuth/WebParameterCollection.cs @@ -14,14 +14,14 @@ namespace NzbDrone.Common.OAuth { get { - var parameters = this.Where(p => p.Name.Equals(name)); + var parameters = this.Where(p => p.Name.Equals(name)).ToArray(); if (!parameters.Any()) { return null; } - if (parameters.Count() == 1) + if (parameters.Length == 1) { return parameters.Single(); } diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index b34b530b2..659a69e7f 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -103,7 +103,7 @@ namespace NzbDrone.Core.Datastore { if (!ids.Any()) { - return new List(); + return Array.Empty(); } var result = Query(x => ids.Contains(x.Id)); diff --git a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxy.cs b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxy.cs index 4b5637a5c..5ae04a152 100644 --- a/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/DownloadStation/Proxies/DownloadStationTaskProxy.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Net.Http; using NLog; @@ -64,7 +65,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies catch (DownloadClientException e) { _logger.Error(e); - return new List(); + return Array.Empty(); } } diff --git a/src/NzbDrone.Core/IndexerSearch/NewznabResults.cs b/src/NzbDrone.Core/IndexerSearch/NewznabResults.cs index 2a076f9c5..9943d3d82 100644 --- a/src/NzbDrone.Core/IndexerSearch/NewznabResults.cs +++ b/src/NzbDrone.Core/IndexerSearch/NewznabResults.cs @@ -22,7 +22,7 @@ namespace NzbDrone.Core.IndexerSearch @"(? Releases { get; set; } + public IList Releases { get; set; } private static string RemoveInvalidXMLChars(string text) { diff --git a/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs b/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs index a760bebba..d3b5aab4b 100644 --- a/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs +++ b/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs @@ -148,7 +148,7 @@ namespace NzbDrone.Core.IndexerSearch return spec; } - private async Task> Dispatch(Func> searchAction, SearchCriteriaBase criteriaBase) + private async Task> Dispatch(Func> searchAction, SearchCriteriaBase criteriaBase) { var indexers = _indexerFactory.Enabled(); @@ -168,7 +168,7 @@ namespace NzbDrone.Core.IndexerSearch if (indexers.Count == 0) { _logger.Debug("All provided categories are unsupported by selected indexers: {0}", string.Join(", ", criteriaBase.Categories)); - return new List(); + return Array.Empty(); } } @@ -189,7 +189,7 @@ namespace NzbDrone.Core.IndexerSearch { if (_indexerLimitService.AtQueryLimit((IndexerDefinition)indexer.Definition)) { - return new List(); + return Array.Empty(); } try @@ -224,7 +224,7 @@ namespace NzbDrone.Core.IndexerSearch _logger.Error(e, "Error while searching for {0}", criteriaBase); } - return new List(); + return Array.Empty(); } } } diff --git a/src/NzbDrone.Core/Indexers/IndexerCapabilitiesCategories.cs b/src/NzbDrone.Core/Indexers/IndexerCapabilitiesCategories.cs index f001b792c..24f227b74 100644 --- a/src/NzbDrone.Core/Indexers/IndexerCapabilitiesCategories.cs +++ b/src/NzbDrone.Core/Indexers/IndexerCapabilitiesCategories.cs @@ -93,7 +93,7 @@ namespace NzbDrone.Core.Indexers { if (string.IsNullOrWhiteSpace(input)) { - return new List(); + return Array.Empty(); } var cats = _categoryMapping @@ -109,7 +109,7 @@ namespace NzbDrone.Core.Indexers { if (string.IsNullOrWhiteSpace(trackerCategoryDesc)) { - return new List(); + return Array.Empty(); } var cats = _categoryMapping diff --git a/src/NzbDrone.Core/Indexers/IndexerFactory.cs b/src/NzbDrone.Core/Indexers/IndexerFactory.cs index 8fbf28b7d..93eab492c 100644 --- a/src/NzbDrone.Core/Indexers/IndexerFactory.cs +++ b/src/NzbDrone.Core/Indexers/IndexerFactory.cs @@ -189,7 +189,7 @@ namespace NzbDrone.Core.Indexers public override IEnumerable GetPresetDefinitions(IndexerDefinition providerDefinition) { - return new List(); + return Array.Empty(); } public override void SetProviderCharacteristics(IIndexer provider, IndexerDefinition definition) diff --git a/src/NzbDrone.Core/Indexers/RssParser.cs b/src/NzbDrone.Core/Indexers/RssParser.cs index e9ca3d146..b24a92020 100644 --- a/src/NzbDrone.Core/Indexers/RssParser.cs +++ b/src/NzbDrone.Core/Indexers/RssParser.cs @@ -82,7 +82,12 @@ namespace NzbDrone.Core.Indexers } } - return !PostProcess(indexerResponse, items, releases) ? new List() : releases; + if (!PostProcess(indexerResponse, items, releases)) + { + return Array.Empty(); + } + + return releases; } public Action, DateTime?> CookiesUpdater { get; set; } diff --git a/src/Prowlarr.Api.V1/ProviderControllerBase.cs b/src/Prowlarr.Api.V1/ProviderControllerBase.cs index ea37ea535..0c61e5ed3 100644 --- a/src/Prowlarr.Api.V1/ProviderControllerBase.cs +++ b/src/Prowlarr.Api.V1/ProviderControllerBase.cs @@ -44,11 +44,11 @@ namespace Prowlarr.Api.V1 [Produces("application/json")] public List GetAll() { - var providerDefinitions = _providerFactory.All().OrderBy(p => p.ImplementationName); + var providerDefinitions = _providerFactory.All(); - var result = new List(providerDefinitions.Count()); + var result = new List(providerDefinitions.Count); - foreach (var definition in providerDefinitions) + foreach (var definition in providerDefinitions.OrderBy(p => p.ImplementationName)) { _providerFactory.SetProviderCharacteristics(definition); diff --git a/src/Prowlarr.Http/REST/RestController.cs b/src/Prowlarr.Http/REST/RestController.cs index 3c2bbd527..d1d07576e 100644 --- a/src/Prowlarr.Http/REST/RestController.cs +++ b/src/Prowlarr.Http/REST/RestController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using FluentValidation; using FluentValidation.Results; using Microsoft.AspNetCore.Mvc; @@ -73,7 +74,8 @@ namespace Prowlarr.Http.REST } } - var attributes = descriptor.MethodInfo.CustomAttributes; + var attributes = descriptor.MethodInfo.CustomAttributes as IReadOnlyCollection ?? + descriptor.MethodInfo.CustomAttributes.ToArray(); if (attributes.Any(x => VALIDATE_ID_ATTRIBUTES.Contains(x.AttributeType)) && !skipValidate) { if (context.ActionArguments.TryGetValue("id", out var idObj))