Skip to content

Commit

Permalink
Enable new analyzer CA1868: 'Unnecessary call to 'Contains' for sets'…
Browse files Browse the repository at this point in the history
… and fix findings (#89652)
  • Loading branch information
mpidash committed Jul 29, 2023
1 parent d5c4a4e commit 4ed3550
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 19 deletions.
3 changes: 3 additions & 0 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions eng/CodeAnalysis.test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,8 @@ void IDependencyAnalyzerLogNodeVisitor<DependencyContextType>.VisitCombinedNode(
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,7 @@ internal override bool TryGetExports(ImportDefinition definition, out Tuple<Comp
}
else
{
if (candidates.Contains(candidatePart))
{
alreadyProcessed = true;
}
else
{
candidates.Add(candidatePart);
}
alreadyProcessed |= !candidates.Add(candidatePart);
}
if (!alreadyProcessed)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,8 @@ private void IncrementRefCount(string clientId, EventCommandEventArgs command)
}
}

if (!_sharedSessionClientIds.Contains(clientId))
if (_sharedSessionClientIds.Add(clientId))
{
_sharedSessionClientIds.Add(clientId);
Interlocked.Increment(ref _sharedSessionRefCount);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/tasks/MobileBuildTasks/Android/AndroidProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ private static string BuildClangArgs(AndroidBuildOptions buildOptions)
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} ");
Expand Down
3 changes: 1 addition & 2 deletions src/tools/illink/src/ILLink.Shared/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ private static DynamicallyAccessedMemberTypes[] GetAllDynamicallyAccessedMemberT
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 ();
}

Expand Down

0 comments on commit 4ed3550

Please sign in to comment.