Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ dotnet_diagnostic.CA1852.severity = warning
# CA1853: Unnecessary call to 'Dictionary.ContainsKey(key)'
dotnet_diagnostic.CA1853.severity = warning

# CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method
dotnet_diagnostic.CA1854.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 @@ -408,6 +408,9 @@ dotnet_diagnostic.CA1852.severity = none
# CA1853: Unnecessary call to 'Dictionary.ContainsKey(key)'
dotnet_diagnostic.CA1853.severity = none

# CA1854: Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method
dotnet_diagnostic.CA1854.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 @@ -46,9 +46,9 @@ public static void Create(MetadataReader metadataReader, string clsidMapPath)
Guid guid = GetTypeGuid(metadataReader, definition);
string guidString = GetTypeGuid(metadataReader, definition).ToString("B");

if (clsidMap.ContainsKey(guidString))
if (clsidMap.TryGetValue(guidString, out ClsidEntry value))
{
throw new ConflictingGuidException(clsidMap[guidString].Type, GetTypeName(metadataReader, definition), guid);
throw new ConflictingGuidException(value.Type, GetTypeName(metadataReader, definition), guid);
}

string progId = GetProgId(metadataReader, definition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,8 @@ private static void RaiseEvent(bool checkFinalization, object key, params object

lock (s_eventLockObject)
{
if (s_handlers != null && s_handlers.ContainsKey(key))
if (s_handlers != null && s_handlers.TryGetValue(key, out List<SystemEventInvokeInfo>? invokeItems))
{
List<SystemEventInvokeInfo> invokeItems = s_handlers[key];

// clone the list so we don't have this type locked and cause
// a deadlock if someone tries to modify handlers during an invoke.
if (invokeItems != null)
Expand Down Expand Up @@ -1068,10 +1066,8 @@ private static void RemoveEventHandler(object key, Delegate? value)

lock (s_eventLockObject)
{
if (s_handlers != null && s_handlers.ContainsKey(key))
if (s_handlers != null && s_handlers.TryGetValue(key, out List<SystemEventInvokeInfo>? invokeItems))
{
List<SystemEventInvokeInfo> invokeItems = s_handlers[key];

invokeItems.Remove(new SystemEventInvokeInfo(value));
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,8 @@ private static Assembly SgenAssemblyResolver(object source, ResolveEventArgs e)
return null;
}

if (s_referencedic.ContainsKey(assemblyname))
if (s_referencedic.TryGetValue(assemblyname, out string reference))
{
string reference = s_referencedic[assemblyname];

// For System.ServiceModel.Primitives, we need to load its runtime assembly rather than reference assembly
if (assemblyname.Equals("System.ServiceModel.Primitives"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public AttributedExportDefinition(AttributedPartCreationInfo partCreationInfo, M
metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity);

var partMetadata = _partCreationInfo.GetMetadata();
if (partMetadata != null && partMetadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName))
if (partMetadata != null && partMetadata.TryGetValue(CompositionConstants.PartCreationPolicyMetadataName, out object? value))
{
metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, partMetadata[CompositionConstants.PartCreationPolicyMetadataName]);
metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, value);
}

if ((_typeIdentityType != null) && (_member.MemberType != MemberTypes.Method) && _typeIdentityType.ContainsGenericParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,8 @@ private static void ApplyValidator(ConfigurationElement elem)
{
Debug.Assert(elem != null);

if ((s_perTypeValidators != null) && s_perTypeValidators.ContainsKey(elem.GetType()))
elem._elementProperty = new ConfigurationElementProperty(s_perTypeValidators[elem.GetType()]);
if ((s_perTypeValidators != null) && s_perTypeValidators.TryGetValue(elem.GetType(), out ConfigurationValidatorBase value))
elem._elementProperty = new ConfigurationElementProperty(value);
}

protected void SetPropertyValue(ConfigurationProperty prop, object value, bool ignoreLocks)
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,9 +1349,9 @@ internal void HandleKeyref(XmlSchemaKeyref keyref)

if (FromInference && relation.Nested)
{
if (_tableDictionary!.ContainsKey(relation.ParentTable))
if (_tableDictionary!.TryGetValue(relation.ParentTable, out List<DataTable>? value))
{
_tableDictionary[relation.ParentTable].Add(relation.ChildTable);
value.Add(relation.ChildTable);
}
}

Expand Down Expand Up @@ -1762,9 +1762,9 @@ internal DataTable InstantiateTable(XmlSchemaElement node, XmlSchemaComplexType
_tableChild.DataSet!.Relations.Add(relation);
if (FromInference && relation.Nested)
{
if (_tableDictionary!.ContainsKey(relation.ParentTable))
if (_tableDictionary!.TryGetValue(relation.ParentTable, out List<DataTable>? value))
{
_tableDictionary[relation.ParentTable].Add(relation.ChildTable);
value.Add(relation.ChildTable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ internal bool HasBlankPassword
{
if (!ConvertValueToIntegratedSecurity())
{
if (_parsetable.ContainsKey(KEY.Password))
if (_parsetable.TryGetValue(KEY.Password, out string? value))
{
return string.IsNullOrEmpty(_parsetable[KEY.Password]);
return string.IsNullOrEmpty(value);
}
else
if (_parsetable.ContainsKey(SYNONYM.Pwd))
else if (_parsetable.TryGetValue(SYNONYM.Pwd, out string? val))
{
return string.IsNullOrEmpty(_parsetable[SYNONYM.Pwd]); // MDAC 83097
return string.IsNullOrEmpty(val); // MDAC 83097
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ private void BuildExtensionPropertyList(Hashtable propertyList, Type p)

protected void BuildPropertySet(Type p, StringCollection propertySet)
{
if (TypeToLdapPropListMap[this.MappingTableIndex].ContainsKey(p))
if (TypeToLdapPropListMap[this.MappingTableIndex].TryGetValue(p, out StringCollection value))
{
Debug.Assert(TypeToLdapPropListMap[this.MappingTableIndex].ContainsKey(p));
string[] props = new string[TypeToLdapPropListMap[this.MappingTableIndex][p].Count];
TypeToLdapPropListMap[this.MappingTableIndex][p].CopyTo(props, 0);
string[] props = new string[value.Count];
value.CopyTo(props, 0);
propertySet.AddRange(props);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ public void DeletePart(Uri partUri)

PackUriHelper.ValidatedPartUri validatedPartUri = (PackUriHelper.ValidatedPartUri)PackUriHelper.ValidatePartUri(partUri);

if (_partList.ContainsKey(validatedPartUri))
if (_partList.TryGetValue(validatedPartUri, out PackagePart? value))
{
//This will get the actual casing of the part that
//is stored in the partList which is equivalent to the
//partUri provided by the user
validatedPartUri = (PackUriHelper.ValidatedPartUri)_partList[validatedPartUri].Uri;
validatedPartUri = (PackUriHelper.ValidatedPartUri)value.Uri;
_partList[validatedPartUri].IsDeleted = true;
_partList[validatedPartUri].Close();

Expand Down Expand Up @@ -1125,9 +1125,9 @@ private bool DoCloseRelationshipsXml(PackagePart p)

PackUriHelper.ValidatedPartUri validatePartUri = PackUriHelper.ValidatePartUri(partUri);

if (_partList.ContainsKey(validatePartUri))
if (_partList.TryGetValue(validatePartUri, out PackagePart? value))
{
return _partList[validatePartUri];
return value;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,8 @@ internal void AddContentType(PackUriHelper.ValidatedPartUri partUri, ContentType

// Need to create an override entry?
if (extension.Length == 0
|| (_defaultDictionary.ContainsKey(extension)
&& !(foundMatchingDefault =
_defaultDictionary[extension].AreTypeAndSubTypeEqual(contentType))))
|| (_defaultDictionary.TryGetValue(extension, out ContentType? value)
&& !(foundMatchingDefault = value.AreTypeAndSubTypeEqual(contentType))))
{
AddOverrideElement(partUri, contentType);
}
Expand All @@ -629,16 +628,16 @@ internal void AddContentType(PackUriHelper.ValidatedPartUri partUri, ContentType
//partUri provided. Override takes precedence over the default entries
if (_overrideDictionary != null)
{
if (_overrideDictionary.ContainsKey(partUri))
return _overrideDictionary[partUri];
if (_overrideDictionary.TryGetValue(partUri, out ContentType? val))
return val;
}

//Step 2: Check if there is a default entry corresponding to the
//extension of the partUri provided.
string extension = partUri.PartUriExtension;

if (_defaultDictionary.ContainsKey(extension))
return _defaultDictionary[extension];
if (_defaultDictionary.TryGetValue(extension, out ContentType? value))
return value;

//Step 3: If we did not find an entry in the override and the default
//dictionaries, this is an error condition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ internal IDictionary<string, string> GetNamespacesInScope(XmlNamespaceScope scop

if (scope != XmlNamespaceScope.Local)
{
if (dict.ContainsKey(string.Empty) && dict[string.Empty] == string.Empty)
if (dict.TryGetValue(string.Empty, out string? value) && value == string.Empty)
{
dict.Remove(string.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ internal Rule FindRule(string sRule)

System.Diagnostics.Debug.Assert(dwSymbolOffset == 0 || _symbols[iWord] == sRule);

rule = dwSymbolOffset > 0 && _nameOffsetRules.ContainsKey(dwSymbolOffset) ? _nameOffsetRules[dwSymbolOffset] : null;
rule = dwSymbolOffset > 0 && _nameOffsetRules.TryGetValue(dwSymbolOffset, out Rule value) ? value : null;
}
}

Expand Down