Enable new analyzer CA1868: 'Unnecessary call to 'Contains' for sets' and fix finding...
authorMario Pistrich <mario@pistrich.com>
Sat, 29 Jul 2023 07:41:35 +0000 (09:41 +0200)
committerGitHub <noreply@github.com>
Sat, 29 Jul 2023 07:41:35 +0000 (09:41 +0200)
eng/CodeAnalysis.src.globalconfig
eng/CodeAnalysis.test.globalconfig
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ModuleInitializerListNode.cs
src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DgmlWriter.cs
src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/MetricsEventSource.cs
src/tasks/MobileBuildTasks/Android/AndroidProject.cs
src/tools/illink/src/ILLink.Shared/Annotations.cs

index de0d6e8..fed3f10 100644 (file)
@@ -483,6 +483,9 @@ dotnet_diagnostic.CA1863.severity = suggestion
 # CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
 dotnet_diagnostic.CA1864.severity = warning
 
+# CA1868: Unnecessary call to 'Contains' for sets
+dotnet_diagnostic.CA1868.severity = warning
+
 # CA2000: Dispose objects before losing scope
 dotnet_diagnostic.CA2000.severity = none
 
index 3312010..baa75bc 100644 (file)
@@ -480,6 +480,9 @@ dotnet_diagnostic.CA1863.severity = none
 # CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
 dotnet_diagnostic.CA1864.severity = none
 
+# CA1868: Unnecessary call to 'Contains' for sets
+dotnet_diagnostic.CA1868.severity = none
+
 # CA2000: Dispose objects before losing scope
 dotnet_diagnostic.CA2000.severity = none
 
index 185274a..c85f149 100644 (file)
@@ -113,9 +113,8 @@ namespace ILCompiler.DependencyAnalysis
                 {
                     foreach (var module in allModules)
                     {
-                        if (!markedModules.Contains(module))
+                        if (markedModules.Add(module))
                         {
-                            markedModules.Add(module);
                             if (modulesWithCctor.Contains(module.Module))
                                 sortedModules.Add(module.Module);
                             break;
index a6018ed..a057039 100644 (file)
@@ -158,10 +158,8 @@ namespace ILCompiler.DependencyAnalysisFramework
         void IDependencyAnalyzerLogEdgeVisitor<DependencyContextType>.VisitEdge(DependencyNodeCore<DependencyContextType> nodeDepender, DependencyNodeCore<DependencyContextType> nodeDependerOther, DependencyNodeCore<DependencyContextType> nodeDependedOn, string reason)
         {
             var combinedNode = new Tuple<DependencyNodeCore<DependencyContextType>, DependencyNodeCore<DependencyContextType>>(nodeDepender, nodeDependerOther);
-            if (!_combinedNodesEdgeVisited.Contains(combinedNode))
+            if (_combinedNodesEdgeVisited.Add(combinedNode))
             {
-                _combinedNodesEdgeVisited.Add(combinedNode);
-
                 _xmlWrite.WriteStartElement("Link");
                 _xmlWrite.WriteAttributeString("Source", _nodeMappings[nodeDepender].ToString());
                 _xmlWrite.WriteAttributeString("Target", _nodeMappings[combinedNode].ToString());
index 3fba07e..fbe5a2f 100644 (file)
@@ -185,14 +185,7 @@ namespace System.ComponentModel.Composition.ReflectionModel
                                 }
                                 else
                                 {
-                                    if (candidates.Contains(candidatePart))
-                                    {
-                                        alreadyProcessed = true;
-                                    }
-                                    else
-                                    {
-                                        candidates.Add(candidatePart);
-                                    }
+                                    alreadyProcessed |= !candidates.Add(candidatePart);
                                 }
                                 if (!alreadyProcessed)
                                 {
index 90aef63..3374c2c 100644 (file)
@@ -472,9 +472,8 @@ namespace System.Diagnostics.Metrics
                     }
                 }
 
-                if (!_sharedSessionClientIds.Contains(clientId))
+                if (_sharedSessionClientIds.Add(clientId))
                 {
-                    _sharedSessionClientIds.Add(clientId);
                     Interlocked.Increment(ref _sharedSessionRefCount);
                 }
             }
index eb41d45..2423a2d 100644 (file)
@@ -122,9 +122,8 @@ namespace Microsoft.Android.Build
                 string rootPath = Path.GetDirectoryName(lib)!;
                 string libName = Path.GetFileName(lib);
 
-                if (!libDirs.Contains(rootPath))
+                if (libDirs.Add(rootPath))
                 {
-                    libDirs.Add(rootPath);
                     ret.Append($"-L {rootPath} ");
                 }
                 ret.Append($"-l:{libName} ");
index 78bbf44..3eaf837 100644 (file)
@@ -79,8 +79,7 @@ namespace ILLink.Shared
                        var values = new HashSet<DynamicallyAccessedMemberTypes> (
                                                                Enum.GetValues (typeof (DynamicallyAccessedMemberTypes))
                                                                .Cast<DynamicallyAccessedMemberTypes> ());
-                       if (!values.Contains (DynamicallyAccessedMemberTypes.Interfaces))
-                               values.Add (DynamicallyAccessedMemberTypes.Interfaces);
+                       values.Add (DynamicallyAccessedMemberTypes.Interfaces);
                        return values.ToArray ();
                }