Fix warnings found with new analyzer CA1860: Prefer Length/Count/IsEmpty property...
authorBuyaa Namnan <bunamnan@microsoft.com>
Tue, 7 Feb 2023 12:59:20 +0000 (04:59 -0800)
committerGitHub <noreply@github.com>
Tue, 7 Feb 2023 12:59:20 +0000 (13:59 +0100)
Co-authored-by: Marek Safar <marek.safar@gmail.com>
17 files changed:
eng/CodeAnalysis.src.globalconfig
eng/CodeAnalysis.test.globalconfig
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Logging/DocumentationSignatureParser.cs
src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextJsonReader.cs
src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/Internal/MatcherContext.cs
src/libraries/Microsoft.NETCore.Platforms/src/GenerateRuntimeGraph.cs
src/libraries/System.ComponentModel.Composition.Registration/src/System/ComponentModel/Composition/Registration/PartBuilder.cs
src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/CatalogExportProvider.cs
src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Hosting/ImportEngine.PartManager.cs
src/libraries/System.Composition.Convention/src/System/Composition/Convention/PartConventionBuilder.cs
src/libraries/System.Composition.TypedParts/src/System/Composition/TypedParts/Discovery/DiscoveredPart.cs
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs
src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
src/tasks/AndroidAppBuilder/ApkBuilder.cs
src/tasks/AppleAppBuilder/AppleAppBuilder.cs
src/tasks/Common/DexBuilder.cs
src/tasks/WorkloadBuildTasks/PackageInstaller.cs

index 861e63f..cf42371 100644 (file)
@@ -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
 
index 702ef11..15601fc 100644 (file)
@@ -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
 
index fd03aa7..706b9bc 100644 (file)
@@ -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();
index e305a03..a6e80f8 100644 (file)
@@ -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,
index 6fda5e7..b213474 100644 (file)
@@ -61,7 +61,7 @@ namespace Microsoft.Extensions.FileSystemGlobbing.Internal
             Declare();
 
             var entities = new List<FileSystemInfoBase?>();
-            if (_declaredWildcardPathSegment || _declaredLiteralFileSegments.Any())
+            if (_declaredWildcardPathSegment || _declaredLiteralFileSegments.Count != 0)
             {
                 entities.AddRange(directory.EnumerateFileSystemInfos());
             }
index 9ee999c..314dd59 100644 (file)
@@ -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;
index b9943ae..2ad43f5 100644 (file)
@@ -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<Tuple<object, List<Attribute>>> configuredMembers)
         {
-            if (_propertyImports.Any() || _propertyExports.Any())
+            if (_propertyImports.Count != 0 || _propertyExports.Count != 0)
             {
                 foreach (PropertyInfo pi in type.GetProperties())
                 {
index 7799abe..f950436 100644 (file)
@@ -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<ExportDefinition>(), localAtomicComposition));
index b04d379..5f43401 100644 (file)
@@ -159,7 +159,7 @@ namespace System.ComponentModel.Composition.Hosting
                     if (disposableExports == null)
                     {
                         _importedDisposableExports.Remove(import);
-                        if (!_importedDisposableExports.Any())
+                        if (_importedDisposableExports.Count == 0)
                         {
                             _importedDisposableExports = null;
                         }
index 770877c..fa7b93b 100644 (file)
@@ -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<Tuple<object, List<Attribute>>> configuredMembers)
         {
-            if (_propertyImports.Any() || _propertyExports.Any())
+            if (_propertyImports.Count != 0 || _propertyExports.Count != 0)
             {
                 foreach (PropertyInfo pi in type.GetRuntimeProperties())
                 {
index d8e9713..47cefcc 100644 (file)
@@ -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)
                 {
index 72aa90c..5791bd0 100644 (file)
@@ -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)
index e910a74..0f9385e 100644 (file)
@@ -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}");
index d5fd824..9b73fae 100644 (file)
@@ -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);
index c375de7..859db0b 100644 (file)
@@ -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.");
         }
index a98da4e..33fdcc8 100644 (file)
@@ -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);
index 45efeef..d5646a3 100644 (file)
@@ -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)