Skip to content

Commit

Permalink
Merge pull request #69179 from mpidash/remove-guarded-set-calls
Browse files Browse the repository at this point in the history
Remove unnecessary calls to `Contains` for sets
  • Loading branch information
sharwell authored Jul 25, 2023
2 parents 387b6fb + 98a0ae0 commit d4bb429
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 25 deletions.
1 change: 1 addition & 0 deletions eng/config/globalconfigs/Common.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotnet_diagnostic.CA1067.severity = warning
dotnet_diagnostic.CA1068.severity = warning
dotnet_diagnostic.CA1200.severity = warning
dotnet_diagnostic.CA1821.severity = warning
dotnet_diagnostic.CA1865.severity = warning
dotnet_diagnostic.CA2009.severity = warning

# CA2012: Use ValueTasks correctly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,7 @@ public void ISet_Generic_SymmetricExceptWith_AfterRemovingElements(EnumerableTyp
{
ISet<T> set = GenericISetFactory(setLength);
T value = CreateT(532);
if (!set.Contains(value))
set.Add(value);
set.Add(value);
set.Remove(value);
IEnumerable<T> enumerable = CreateEnumerable(enumerableType, set, enumerableLength, numberOfMatchingElements, numberOfDuplicateElements);
Debug.Assert(enumerable != null);
Expand Down
9 changes: 2 additions & 7 deletions src/Compilers/Core/Portable/MetadataReader/MetadataDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2070,10 +2070,8 @@ private MethodSymbol FindMethodSymbolInSuperType(TypeDefinitionHandle searchType
TypeDefinitionHandle typeDef = typeDefsToSearch.Dequeue();
Debug.Assert(!typeDef.IsNil);

if (!visitedTypeDefTokens.Contains(typeDef))
if (visitedTypeDefTokens.Add(typeDef))
{
visitedTypeDefTokens.Add(typeDef);

foreach (MethodDefinitionHandle methodDef in Module.GetMethodsOfTypeOrThrow(typeDef))
{
if (methodDef == targetMethodDef)
Expand All @@ -2091,12 +2089,9 @@ private MethodSymbol FindMethodSymbolInSuperType(TypeDefinitionHandle searchType
TypeSymbol typeSymbol = typeSymbolsToSearch.Dequeue();
Debug.Assert(typeSymbol != null);

if (!visitedTypeSymbols.Contains(typeSymbol))
if (visitedTypeSymbols.Add(typeSymbol))
{
visitedTypeSymbols.Add(typeSymbol);

//we're looking for a method def but we're currently on a type *ref*, so just enqueue supertypes

EnqueueTypeSymbolInterfacesAndBaseTypes(typeDefsToSearch, typeSymbolsToSearch, typeSymbol);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1802,9 +1802,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
Dim candidateLocation As Location = candidate.NonMergedLocation
Debug.Assert(candidateLocation IsNot Nothing)

If partialMethods.Contains(candidate) Then
If partialMethods.Remove(candidate) Then
' partial-partial conflict
partialMethods.Remove(candidate)

' the 'best' partial method is the one with the 'smallest'
' location, we should report errors on the other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ void ITextViewConnectionListener.SubjectBuffersDisconnected(ITextView textView,
if (reason == ConnectionReason.TextViewLifetime ||
!textView.BufferGraph.GetTextBuffers(b => b.ContentType.IsOfType(ContentTypeNames.RoslynContentType)).Any())
{
if (_subscribedViews.Contains(textView))
if (_subscribedViews.Remove(textView))
{
_subscribedViews.Remove(textView);
textView.Caret.PositionChanged -= OnTextViewCaretPositionChanged;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ internal void Remove()
{
var parameterToRemove = AllParameters[_selectedIndex!.Value];

if (_parametersWithoutDefaultValues.Contains(parameterToRemove))
{
_parametersWithoutDefaultValues.Remove(parameterToRemove);
}
else
if (!_parametersWithoutDefaultValues.Remove(parameterToRemove))
{
_parametersWithDefaultValues.Remove(parameterToRemove);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ public TValue Remove(TKey key)
{
this.AssertIsForeground();

if (_deadKeySet.Contains(key))
{
_deadKeySet.Remove(key);
}
_deadKeySet.Remove(key);

if (_table.TryGetValue(key, out var handle))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.ProjectSystemShim
' To keep things simple, we'll just remove everything and add everything back in
For Each oldRuntimeLibrary In oldRuntimeLibraries
' If this one was added explicitly in addition to our computation, we don't have to remove it
If _explicitlyAddedRuntimeLibraries.Contains(oldRuntimeLibrary) Then
_explicitlyAddedRuntimeLibraries.Remove(oldRuntimeLibrary)
Else
If Not _explicitlyAddedRuntimeLibraries.Remove(oldRuntimeLibrary) Then
ProjectSystemProject.RemoveMetadataReference(oldRuntimeLibrary, MetadataReferenceProperties.Assembly)
End If
Next
Expand Down

0 comments on commit d4bb429

Please sign in to comment.