From 6450398d885ffe65bce3f7c38588dee197738b43 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Tue, 7 Feb 2023 04:59:20 -0800 Subject: [PATCH] Fix warnings found with new analyzer CA1860: Prefer Length/Count/IsEmpty property check over Any() (#81583) Co-authored-by: Marek Safar --- eng/CodeAnalysis.src.globalconfig | 3 +++ eng/CodeAnalysis.test.globalconfig | 3 +++ .../Compiler/Logging/DocumentationSignatureParser.cs | 2 +- .../src/DependencyContextJsonReader.cs | 4 ++-- .../src/Internal/MatcherContext.cs | 2 +- src/libraries/Microsoft.NETCore.Platforms/src/GenerateRuntimeGraph.cs | 2 +- .../src/System/ComponentModel/Composition/Registration/PartBuilder.cs | 4 ++-- .../ComponentModel/Composition/Hosting/CatalogExportProvider.cs | 2 +- .../ComponentModel/Composition/Hosting/ImportEngine.PartManager.cs | 2 +- .../src/System/Composition/Convention/PartConventionBuilder.cs | 4 ++-- .../src/System/Composition/TypedParts/Discovery/DiscoveredPart.cs | 2 +- .../Marshalling/ElementsMarshalling.cs | 2 +- src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs | 2 +- src/tasks/AndroidAppBuilder/ApkBuilder.cs | 2 +- src/tasks/AppleAppBuilder/AppleAppBuilder.cs | 2 +- src/tasks/Common/DexBuilder.cs | 2 +- src/tasks/WorkloadBuildTasks/PackageInstaller.cs | 2 +- 17 files changed, 24 insertions(+), 18 deletions(-) diff --git a/eng/CodeAnalysis.src.globalconfig b/eng/CodeAnalysis.src.globalconfig index 861e63f..cf42371 100644 --- a/eng/CodeAnalysis.src.globalconfig +++ b/eng/CodeAnalysis.src.globalconfig @@ -468,6 +468,9 @@ dotnet_diagnostic.CA1858.severity = warning # CA1859: Use concrete types when possible for improved performance dotnet_diagnostic.CA1859.severity = warning +# CA1860: Prefer Length/Count/IsEmpty property check over Any() +dotnet_diagnostic.CA1860.severity = warning + # CA2000: Dispose objects before losing scope dotnet_diagnostic.CA2000.severity = none diff --git a/eng/CodeAnalysis.test.globalconfig b/eng/CodeAnalysis.test.globalconfig index 702ef11..15601fcd 100644 --- a/eng/CodeAnalysis.test.globalconfig +++ b/eng/CodeAnalysis.test.globalconfig @@ -465,6 +465,9 @@ dotnet_diagnostic.CA1858.severity = none # CA1859: Use concrete types when possible for improved performance dotnet_diagnostic.CA1859.severity = none +# CA1860: Prefer Length/Count/IsEmpty property check over Any() +dotnet_diagnostic.CA1860.severity = none + # CA2000: Dispose objects before losing scope dotnet_diagnostic.CA2000.severity = none diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Logging/DocumentationSignatureParser.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Logging/DocumentationSignatureParser.cs index fd03aa7..706b9bc 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Logging/DocumentationSignatureParser.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Logging/DocumentationSignatureParser.cs @@ -164,7 +164,7 @@ namespace ILCompiler.Logging var typeOrNamespaceName = nameBuilder.ToString(); GetMatchingTypes(module, declaringType: containingType, name: typeOrNamespaceName, arity: arity, results: results); Debug.Assert(results.Count <= 1); - if (results.Any()) + if (results.Count != 0) { // the name resolved to a type var result = results.Single(); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs index e305a03..a6e80f8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs @@ -780,7 +780,7 @@ namespace Microsoft.Extensions.DependencyModel .Select(e => new RuntimeFile(e.Path, e.AssemblyVersion, e.FileVersion)) .ToArray(); - if (groupRuntimeAssemblies.Any()) + if (groupRuntimeAssemblies.Length != 0) { runtimeAssemblyGroups.Add(new RuntimeAssetGroup( ridGroup.Key, @@ -792,7 +792,7 @@ namespace Microsoft.Extensions.DependencyModel .Select(e => new RuntimeFile(e.Path, e.AssemblyVersion, e.FileVersion)) .ToArray(); - if (groupNativeLibraries.Any()) + if (groupNativeLibraries.Length != 0) { nativeLibraryGroups.Add(new RuntimeAssetGroup( ridGroup.Key, diff --git a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/MatcherContext.cs b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/MatcherContext.cs index 6fda5e7..b213474 100644 --- a/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/MatcherContext.cs +++ b/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/MatcherContext.cs @@ -61,7 +61,7 @@ namespace Microsoft.Extensions.FileSystemGlobbing.Internal Declare(); var entities = new List(); - if (_declaredWildcardPathSegment || _declaredLiteralFileSegments.Any()) + if (_declaredWildcardPathSegment || _declaredLiteralFileSegments.Count != 0) { entities.AddRange(directory.EnumerateFileSystemInfos()); } diff --git a/src/libraries/Microsoft.NETCore.Platforms/src/GenerateRuntimeGraph.cs b/src/libraries/Microsoft.NETCore.Platforms/src/GenerateRuntimeGraph.cs index 9ee999c..314dd59 100644 --- a/src/libraries/Microsoft.NETCore.Platforms/src/GenerateRuntimeGraph.cs +++ b/src/libraries/Microsoft.NETCore.Platforms/src/GenerateRuntimeGraph.cs @@ -130,7 +130,7 @@ namespace Microsoft.NETCore.Platforms.BuildTasks public override bool Execute() { - if (RuntimeGroups != null && RuntimeGroups.Any() && RuntimeJson == null) + if (RuntimeGroups != null && RuntimeGroups.Length != 0 && RuntimeJson == null) { Log.LogError($"{nameof(RuntimeJson)} argument must be specified when {nameof(RuntimeGroups)} is specified."); return false; diff --git a/src/libraries/System.ComponentModel.Composition.Registration/src/System/ComponentModel/Composition/Registration/PartBuilder.cs b/src/libraries/System.ComponentModel.Composition.Registration/src/System/ComponentModel/Composition/Registration/PartBuilder.cs index b9943ae..2ad43f5 100644 --- a/src/libraries/System.ComponentModel.Composition.Registration/src/System/ComponentModel/Composition/Registration/PartBuilder.cs +++ b/src/libraries/System.ComponentModel.Composition.Registration/src/System/ComponentModel/Composition/Registration/PartBuilder.cs @@ -319,7 +319,7 @@ namespace System.ComponentModel.Composition.Registration } } - if (_interfaceExports.Any()) + if (_interfaceExports.Count != 0) { if (_typeExportBuilders != null) { @@ -443,7 +443,7 @@ namespace System.ComponentModel.Composition.Registration internal void BuildPropertyAttributes(Type type, ref List>> configuredMembers) { - if (_propertyImports.Any() || _propertyExports.Any()) + if (_propertyImports.Count != 0 || _propertyExports.Count != 0) { foreach (PropertyInfo pi in type.GetProperties()) { diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/CatalogExportProvider.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/CatalogExportProvider.cs index 7799abe..f950436 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/CatalogExportProvider.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/CatalogExportProvider.cs @@ -841,7 +841,7 @@ namespace System.ComponentModel.Composition.Hosting } // Notify anyone sourcing exports that the resurrected exports have appeared - if (resurrectedExports.Any()) + if (resurrectedExports.Count != 0) { OnExportsChanging( new ExportsChangeEventArgs(resurrectedExports, Array.Empty(), localAtomicComposition)); diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/ImportEngine.PartManager.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/ImportEngine.PartManager.cs index b04d379..5f43401 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/ImportEngine.PartManager.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/ImportEngine.PartManager.cs @@ -159,7 +159,7 @@ namespace System.ComponentModel.Composition.Hosting if (disposableExports == null) { _importedDisposableExports.Remove(import); - if (!_importedDisposableExports.Any()) + if (_importedDisposableExports.Count == 0) { _importedDisposableExports = null; } diff --git a/src/libraries/System.Composition.Convention/src/System/Composition/Convention/PartConventionBuilder.cs b/src/libraries/System.Composition.Convention/src/System/Composition/Convention/PartConventionBuilder.cs index 770877c..fa7b93b 100644 --- a/src/libraries/System.Composition.Convention/src/System/Composition/Convention/PartConventionBuilder.cs +++ b/src/libraries/System.Composition.Convention/src/System/Composition/Convention/PartConventionBuilder.cs @@ -559,7 +559,7 @@ namespace System.Composition.Convention } } - if (_interfaceExports.Any()) + if (_interfaceExports.Count != 0) { if (_typeExportBuilders != null) { @@ -730,7 +730,7 @@ namespace System.Composition.Convention internal void BuildPropertyAttributes(Type type, ref List>> configuredMembers) { - if (_propertyImports.Any() || _propertyExports.Any()) + if (_propertyImports.Count != 0 || _propertyExports.Count != 0) { foreach (PropertyInfo pi in type.GetRuntimeProperties()) { diff --git a/src/libraries/System.Composition.TypedParts/src/System/Composition/TypedParts/Discovery/DiscoveredPart.cs b/src/libraries/System.Composition.TypedParts/src/System/Composition/TypedParts/Discovery/DiscoveredPart.cs index d8e9713..47cefcc 100644 --- a/src/libraries/System.Composition.TypedParts/src/System/Composition/TypedParts/Discovery/DiscoveredPart.cs +++ b/src/libraries/System.Composition.TypedParts/src/System/Composition/TypedParts/Discovery/DiscoveredPart.cs @@ -103,7 +103,7 @@ namespace System.Composition.TypedParts.Discovery _constructor = GetConstructorInfoFromGenericType(_partType); } - _constructor ??= _partType.DeclaredConstructors.FirstOrDefault(ci => ci.IsPublic && !(ci.IsStatic || ci.GetParameters().Any())); + _constructor ??= _partType.DeclaredConstructors.FirstOrDefault(ci => ci.IsPublic && !(ci.IsStatic || ci.GetParameters().Length != 0)); if (_constructor == null) { diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs index 72aa90c..5791bd0 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs @@ -371,7 +371,7 @@ namespace Microsoft.Interop elementStatements.AddRange(_elementMarshaller.Generate(localElementInfo, elementSubContext)); } - if (elementStatements.Any()) + if (elementStatements.Count != 0) { StatementSyntax marshallingStatement = Block( List(_elementMarshaller.Generate(localElementInfo, elementSetupSubContext) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs index e910a74..0f9385e 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs @@ -1710,7 +1710,7 @@ namespace Microsoft.WebAssembly.Diagnostics protected async Task SetBreakpoint(SessionId sessionId, DebugStore store, BreakpointRequest req, bool sendResolvedEvent, bool fromEnC, CancellationToken token) { ExecutionContext context = GetContext(sessionId); - if ((!fromEnC && req.Locations.Any()) || (fromEnC && req.Locations.Any(bp => bp.State == BreakpointState.Active))) + if ((!fromEnC && req.Locations.Count != 0) || (fromEnC && req.Locations.Any(bp => bp.State == BreakpointState.Active))) { if (!fromEnC) Log("debug", $"locations already loaded for {req.Id}"); diff --git a/src/tasks/AndroidAppBuilder/ApkBuilder.cs b/src/tasks/AndroidAppBuilder/ApkBuilder.cs index d5fd824..9b73fae 100644 --- a/src/tasks/AndroidAppBuilder/ApkBuilder.cs +++ b/src/tasks/AndroidAppBuilder/ApkBuilder.cs @@ -420,7 +420,7 @@ public partial class ApkBuilder { string[] classFiles = Directory.GetFiles(Path.Combine(OutputDir, "obj"), "*.class", SearchOption.AllDirectories); - if (!classFiles.Any()) + if (classFiles.Length == 0) throw new InvalidOperationException("Didn't find any .class files"); Utils.RunProcess(logger, d8, $"--no-desugaring {string.Join(" ", classFiles)}", workingDir: OutputDir); diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs index c375de7..859db0b 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs @@ -226,7 +226,7 @@ public class AppleAppBuilderTask : Task } } - if (((!ForceInterpreter && (isDevice || ForceAOT)) && !assemblerFiles.Any())) + if (!ForceInterpreter && (isDevice || ForceAOT) && assemblerFiles.Count == 0) { throw new InvalidOperationException("Need list of AOT files for device builds."); } diff --git a/src/tasks/Common/DexBuilder.cs b/src/tasks/Common/DexBuilder.cs index a98da4e2..33fdcc8 100644 --- a/src/tasks/Common/DexBuilder.cs +++ b/src/tasks/Common/DexBuilder.cs @@ -38,7 +38,7 @@ internal sealed class DexBuilder { string[] classFiles = Directory.GetFiles(inputDir, "*.class", SearchOption.AllDirectories); - if (!classFiles.Any()) + if (classFiles.Length == 0) throw new InvalidOperationException("Didn't find any .class files"); Utils.RunProcess(_logger, _androidSdk.D8Path, $"--no-desugaring {string.Join(" ", classFiles)}", workingDir: _workingDir); diff --git a/src/tasks/WorkloadBuildTasks/PackageInstaller.cs b/src/tasks/WorkloadBuildTasks/PackageInstaller.cs index 45efeef..d5646a3 100644 --- a/src/tasks/WorkloadBuildTasks/PackageInstaller.cs +++ b/src/tasks/WorkloadBuildTasks/PackageInstaller.cs @@ -33,7 +33,7 @@ namespace Microsoft.Workload.Build.Tasks public static bool Install(PackageReference[] references, string nugetConfigContents, string baseTempDir, TaskLoggingHelper logger, bool stopOnMissing=true, string? packagesPath=null) { - if (!references.Any()) + if (references.Length == 0) return true; return new PackageInstaller(nugetConfigContents, baseTempDir, packagesPath, logger) -- 2.7.4