From 61ca87cb48435239d662b75636629c432e5fc8f8 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 9 Jun 2022 15:25:37 -0700 Subject: [PATCH] Update analyzer version and fix new findings (#70157) --- eng/CodeAnalysis.src.globalconfig | 3 +++ eng/CodeAnalysis.test.globalconfig | 3 +++ .../managed/Microsoft.NET.HostModel/ComHost/ClsidMap.cs | 4 ++-- .../src/Microsoft/Win32/SystemEvents.cs | 8 ++------ src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs | 4 +--- .../AttributedModel/AttributedExportDefinition.cs | 4 ++-- .../src/System/Configuration/ConfigurationElement.cs | 4 ++-- .../System.Data.Common/src/System/Data/XMLSchema.cs | 8 ++++---- .../src/Common/System/Data/Common/DbConnectionOptions.cs | 9 ++++----- .../AccountManagement/AD/ADStoreCtx_Query.cs | 7 +++---- .../System.IO.Packaging/src/System/IO/Packaging/Package.cs | 8 ++++---- .../src/System/IO/Packaging/ZipPackage.cs | 13 ++++++------- .../System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs | 2 +- .../System.Speech/src/Internal/SrgsCompiler/BackEnd.cs | 2 +- 14 files changed, 38 insertions(+), 41 deletions(-) diff --git a/eng/CodeAnalysis.src.globalconfig b/eng/CodeAnalysis.src.globalconfig index f246a7b..8104f96 100644 --- a/eng/CodeAnalysis.src.globalconfig +++ b/eng/CodeAnalysis.src.globalconfig @@ -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 diff --git a/eng/CodeAnalysis.test.globalconfig b/eng/CodeAnalysis.test.globalconfig index 97bf611..59fc4e0 100644 --- a/eng/CodeAnalysis.test.globalconfig +++ b/eng/CodeAnalysis.test.globalconfig @@ -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 diff --git a/src/installer/managed/Microsoft.NET.HostModel/ComHost/ClsidMap.cs b/src/installer/managed/Microsoft.NET.HostModel/ComHost/ClsidMap.cs index 9864f8f..a8cbb02 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/ComHost/ClsidMap.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/ComHost/ClsidMap.cs @@ -46,9 +46,9 @@ namespace Microsoft.NET.HostModel.ComHost 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); diff --git a/src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft/Win32/SystemEvents.cs b/src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft/Win32/SystemEvents.cs index da1131c..3cccd9a 100644 --- a/src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft/Win32/SystemEvents.cs +++ b/src/libraries/Microsoft.Win32.SystemEvents/src/Microsoft/Win32/SystemEvents.cs @@ -1004,10 +1004,8 @@ namespace Microsoft.Win32 lock (s_eventLockObject) { - if (s_handlers != null && s_handlers.ContainsKey(key)) + if (s_handlers != null && s_handlers.TryGetValue(key, out List? invokeItems)) { - List 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) @@ -1068,10 +1066,8 @@ namespace Microsoft.Win32 lock (s_eventLockObject) { - if (s_handlers != null && s_handlers.ContainsKey(key)) + if (s_handlers != null && s_handlers.TryGetValue(key, out List? invokeItems)) { - List invokeItems = s_handlers[key]; - invokeItems.Remove(new SystemEventInvokeInfo(value)); } } diff --git a/src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs b/src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs index bcd40f5..4f48657 100644 --- a/src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs +++ b/src/libraries/Microsoft.XmlSerializer.Generator/src/Sgen.cs @@ -618,10 +618,8 @@ namespace Microsoft.XmlSerializer.Generator 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")) { diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/AttributedModel/AttributedExportDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/AttributedModel/AttributedExportDefinition.cs index d5e1c3e..b7c23cc 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/AttributedModel/AttributedExportDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/AttributedModel/AttributedExportDefinition.cs @@ -46,9 +46,9 @@ namespace System.ComponentModel.Composition.AttributedModel 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) diff --git a/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationElement.cs b/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationElement.cs index 50f4dc7..c1a2aad 100644 --- a/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationElement.cs +++ b/src/libraries/System.Configuration.ConfigurationManager/src/System/Configuration/ConfigurationElement.cs @@ -831,8 +831,8 @@ namespace System.Configuration { 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) diff --git a/src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs b/src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs index 30cb353..8343433 100644 --- a/src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs +++ b/src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs @@ -1349,9 +1349,9 @@ namespace System.Data if (FromInference && relation.Nested) { - if (_tableDictionary!.ContainsKey(relation.ParentTable)) + if (_tableDictionary!.TryGetValue(relation.ParentTable, out List? value)) { - _tableDictionary[relation.ParentTable].Add(relation.ChildTable); + value.Add(relation.ChildTable); } } @@ -1762,9 +1762,9 @@ namespace System.Data _tableChild.DataSet!.Relations.Add(relation); if (FromInference && relation.Nested) { - if (_tableDictionary!.ContainsKey(relation.ParentTable)) + if (_tableDictionary!.TryGetValue(relation.ParentTable, out List? value)) { - _tableDictionary[relation.ParentTable].Add(relation.ChildTable); + value.Add(relation.ChildTable); } } } diff --git a/src/libraries/System.Data.Odbc/src/Common/System/Data/Common/DbConnectionOptions.cs b/src/libraries/System.Data.Odbc/src/Common/System/Data/Common/DbConnectionOptions.cs index e36c0d8..3aa0aa5 100644 --- a/src/libraries/System.Data.Odbc/src/Common/System/Data/Common/DbConnectionOptions.cs +++ b/src/libraries/System.Data.Odbc/src/Common/System/Data/Common/DbConnectionOptions.cs @@ -77,14 +77,13 @@ namespace System.Data.Common { 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 { diff --git a/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx_Query.cs b/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx_Query.cs index 1561394..25f3bb8 100644 --- a/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx_Query.cs +++ b/src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx_Query.cs @@ -44,11 +44,10 @@ namespace System.DirectoryServices.AccountManagement 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 diff --git a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/Package.cs b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/Package.cs index d1d3f03..1d33e54 100644 --- a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/Package.cs +++ b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/Package.cs @@ -316,12 +316,12 @@ namespace System.IO.Packaging 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(); @@ -1125,9 +1125,9 @@ namespace System.IO.Packaging PackUriHelper.ValidatedPartUri validatePartUri = PackUriHelper.ValidatePartUri(partUri); - if (_partList.ContainsKey(validatePartUri)) + if (_partList.TryGetValue(validatePartUri, out PackagePart? value)) { - return _partList[validatePartUri]; + return value; } else { diff --git a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs index d8b6a25..0427879 100644 --- a/src/libraries/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs +++ b/src/libraries/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs @@ -606,9 +606,8 @@ namespace System.IO.Packaging // 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); } @@ -629,16 +628,16 @@ namespace System.IO.Packaging //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 diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs index 8c660bc..15de351 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs @@ -1027,7 +1027,7 @@ namespace System.Xml 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); } diff --git a/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs b/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs index 0f678c5..6d0c8d9 100644 --- a/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs +++ b/src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs @@ -342,7 +342,7 @@ namespace System.Speech.Internal.SrgsCompiler 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; } } -- 2.7.4