Fix concurrent collections CA1836 rule violations (#40629)
authorDavid Cantu <dacantu@microsoft.com>
Tue, 11 Aug 2020 01:12:52 +0000 (18:12 -0700)
committerGitHub <noreply@github.com>
Tue, 11 Aug 2020 01:12:52 +0000 (18:12 -0700)
* Fix concurrent collections CA1836 rule violations

* Update dogfood analyzer version

eng/Analyzers.props
eng/CodeAnalysis.ruleset
src/libraries/Common/src/System/Data/ProviderBase/DbConnectionPoolGroup.cs
src/libraries/Microsoft.Extensions.Http/src/DefaultHttpClientFactory.cs
src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolGroup.cs
src/libraries/System.Net.Security/src/System/Net/Security/SslSessionsCache.cs
src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.cs

index c14a869..34eb83d 100644 (file)
@@ -6,7 +6,7 @@
   </PropertyGroup>
   <ItemGroup Condition="'$(EnableAnalyzers)' == 'true'">
     <PackageReference Include="Microsoft.DotNet.CodeAnalysis" Version="$(MicrosoftDotNetCodeAnalysisVersion)" PrivateAssets="all" IsImplicitlyDefined="true" />
-    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="3.3.0-beta3.20407.4" PrivateAssets="all" />
+    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="3.3.0-beta3.20410.1" PrivateAssets="all" />
     <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164" PrivateAssets="all" />
   </ItemGroup>
 </Project>
index 3726904..293d25a 100644 (file)
       <Rule Id="CA1833" Action="Warning" />          <!-- Use AsSpan or AsMemory instead of Range-based indexers when appropriate -->
       <Rule Id="CA1834" Action="Warning" />          <!-- Consider using 'StringBuilder.Append(char)' when applicable. -->
       <Rule Id="CA1835" Action="Warning" />          <!-- Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync' -->
-      <Rule Id="CA1836" Action="Info" />             <!-- Prefer IsEmpty over Count -->
+      <Rule Id="CA1836" Action="Warning" />          <!-- Prefer IsEmpty over Count -->
       <Rule Id="CA1837" Action="Warning" />          <!-- Use 'Environment.ProcessId' -->
       <Rule Id="CA1838" Action="Warning" />          <!-- Avoid 'StringBuilder' parameters for P/Invokes -->
       <Rule Id="CA2000" Action="None" />             <!-- Dispose objects before losing scope -->
index 26ee3b6..a3807e9 100644 (file)
@@ -105,7 +105,7 @@ namespace System.Data.ProviderBase
             ConcurrentDictionary<DbConnectionPoolIdentity, DbConnectionPool>? oldPoolCollection = null;
             lock (this)
             {
-                if (_poolCollection.Count > 0)
+                if (!_poolCollection.IsEmpty)
                 {
                     oldPoolCollection = _poolCollection;
                     _poolCollection = new ConcurrentDictionary<DbConnectionPoolIdentity, DbConnectionPool>();
@@ -232,7 +232,7 @@ namespace System.Data.ProviderBase
             //     to avoid conflict with DbConnectionFactory.CreateConnectionPoolGroup replacing pool entry
             lock (this)
             {
-                if (_poolCollection.Count > 0)
+                if (!_poolCollection.IsEmpty)
                 {
                     var newPoolCollection = new ConcurrentDictionary<DbConnectionPoolIdentity, DbConnectionPool>();
 
@@ -267,7 +267,7 @@ namespace System.Data.ProviderBase
 
                 // must be pruning thread to change state and no connections
                 // otherwise pruning thread risks making entry disabled soon after user calls ClearPool
-                if (0 == _poolCollection.Count)
+                if (_poolCollection.IsEmpty)
                 {
                     if (PoolGroupStateActive == _state)
                     {
index d3ad266..059111a 100644 (file)
@@ -324,7 +324,7 @@ namespace Microsoft.Extensions.Http
             }
 
             // We didn't totally empty the cleanup queue, try again later.
-            if (_expiredHandlers.Count > 0)
+            if (!_expiredHandlers.IsEmpty)
             {
                 StartCleanupTimer();
             }
index 792f6e7..4a77e1c 100644 (file)
@@ -129,7 +129,7 @@ namespace System.Data.ProviderBase
             ConcurrentDictionary<DbConnectionPoolIdentity, DbConnectionPool>? oldPoolCollection = null;
             lock (this)
             {
-                if (_poolCollection.Count > 0)
+                if (!_poolCollection.IsEmpty)
                 {
                     oldPoolCollection = _poolCollection;
                     _poolCollection = new ConcurrentDictionary<DbConnectionPoolIdentity, DbConnectionPool>();
@@ -266,7 +266,7 @@ namespace System.Data.ProviderBase
             //     to avoid conflict with DbConnectionFactory.CreateConnectionPoolGroup replacing pool entry
             lock (this)
             {
-                if (_poolCollection.Count > 0)
+                if (!_poolCollection.IsEmpty)
                 {
                     var newPoolCollection = new ConcurrentDictionary<DbConnectionPoolIdentity, DbConnectionPool>();
 
@@ -309,7 +309,7 @@ namespace System.Data.ProviderBase
 
                 // must be pruning thread to change state and no connections
                 // otherwise pruning thread risks making entry disabled soon after user calls ClearPool
-                if (0 == _poolCollection.Count)
+                if (_poolCollection.IsEmpty)
                 {
                     if (PoolGroupStateActive == _state)
                     {
index ea2c83e..eccd25b 100644 (file)
@@ -115,7 +115,7 @@ namespace System.Net.Security
         //
         internal static SafeFreeCredentials? TryCachedCredential(byte[]? thumbPrint, SslProtocols sslProtocols, bool isServer, EncryptionPolicy encryptionPolicy)
         {
-            if (s_cachedCreds.Count == 0)
+            if (s_cachedCreds.IsEmpty)
             {
                 if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, $"Not found, Current Cache Count = {s_cachedCreds.Count}");
                 return null;
index ac4a037..21cee18 100644 (file)
@@ -318,7 +318,7 @@ namespace System.Threading.Tasks
                     }
 
                     // If we have encountered any exceptions, then throw.
-                    if ((exceptionQ != null) && (exceptionQ.Count > 0))
+                    if ((exceptionQ != null) && (!exceptionQ.IsEmpty))
                     {
                         ThrowSingleCancellationExceptionOrOtherException(exceptionQ, parallelOptions.CancellationToken,
                                                                          new AggregateException(exceptionQ));