Enable new analyzer CA1868: 'Unnecessary call to 'Contains' for sets' and fix findings (#89652)

This commit is contained in:
Mario Pistrich 2023-07-29 09:41:35 +02:00 committed by GitHub
parent d5c4a4e6b7
commit 4ed355098d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 12 additions and 19 deletions

View 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

View 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

View 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;

View 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());

View 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)
{

View File

@ -472,9 +472,8 @@ namespace System.Diagnostics.Metrics
}
}
if (!_sharedSessionClientIds.Contains(clientId))
if (_sharedSessionClientIds.Add(clientId))
{
_sharedSessionClientIds.Add(clientId);
Interlocked.Increment(ref _sharedSessionRefCount);
}
}

View 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} ");

View 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 ();
}