Fixed CA1854 occurrences (#80996)
authorMiizukii <4228320+Miizukii@users.noreply.github.com>
Fri, 27 Jan 2023 15:51:06 +0000 (16:51 +0100)
committerGitHub <noreply@github.com>
Fri, 27 Jan 2023 15:51:06 +0000 (10:51 -0500)
12 files changed:
src/coreclr/tools/Common/CommandLineHelpers.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/MethodBodyScanner.cs
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/ObjectWriter.cs
src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs
src/libraries/System.DirectoryServices.AccountManagement/src/System/DirectoryServices/AccountManagement/AD/ADStoreCtx.cs
src/libraries/System.IO.Packaging/src/System/IO/Packaging/PartBasedPackageProperties.cs
src/libraries/System.Private.Xml/src/System/Xml/XPath/XPathDocument.cs
src/libraries/System.Private.Xml/src/System/Xml/Xsl/IlGen/StaticDataManager.cs
src/libraries/System.Security.AccessControl/src/System/Security/AccessControl/Privilege.cs
src/libraries/System.Speech/src/Internal/GrammarBuilding/BuilderElements.cs
src/libraries/System.Speech/src/Internal/SrgsCompiler/BackEnd.cs
src/tasks/WasmAppBuilder/IcallTableGenerator.cs

index 0dd98b2..d1282b4 100644 (file)
@@ -305,12 +305,12 @@ namespace System.CommandLine
 
                     string simpleName = Path.GetFileNameWithoutExtension(fileName);
 
-                    if (dictionary.ContainsKey(simpleName))
+                    if (dictionary.TryGetValue(simpleName, out string otherFullFileName))
                     {
                         if (strict)
                         {
                             throw new CommandLineException("Multiple input files matching same simple name " +
-                                fullFileName + " " + dictionary[simpleName]);
+                                fullFileName + " " + otherFullFileName);
                         }
                     }
                     else
index 78fd901..fa83eca 100644 (file)
@@ -148,9 +148,9 @@ namespace ILCompiler.Dataflow
                 return;
             }
 
-            if (knownStacks.ContainsKey(newOffset))
+            if (knownStacks.TryGetValue(newOffset, out var knownStack))
             {
-                knownStacks[newOffset] = MergeStack(knownStacks[newOffset], newStack);
+                knownStacks[newOffset] = MergeStack(knownStack, newStack);
             }
             else
             {
@@ -363,16 +363,16 @@ namespace ILCompiler.Dataflow
                 ValidateNoReferenceToReference(locals, methodBody, reader.Offset);
                 int curBasicBlock = blockIterator.MoveNext(reader.Offset);
 
-                if (knownStacks.ContainsKey(reader.Offset))
+                if (knownStacks.TryGetValue(reader.Offset, out var knownStack))
                 {
                     if (currentStack == null)
                     {
                         // The stack copy constructor reverses the stack
-                        currentStack = new Stack<StackSlot>(knownStacks[reader.Offset].Reverse());
+                        currentStack = new Stack<StackSlot>(knownStack.Reverse());
                     }
                     else
                     {
-                        currentStack = MergeStack(currentStack, knownStacks[reader.Offset]);
+                        currentStack = MergeStack(currentStack, knownStack);
                     }
                 }
 
index d6eb8ee..6b728cc 100644 (file)
@@ -766,12 +766,13 @@ namespace ILCompiler.DependencyAnalysis
             _offsetToDefName.Clear();
             foreach (ISymbolDefinitionNode n in definedSymbols)
             {
-                if (!_offsetToDefName.ContainsKey(n.Offset))
+                if (!_offsetToDefName.TryGetValue(n.Offset, out var nodes))
                 {
-                    _offsetToDefName[n.Offset] = new List<ISymbolDefinitionNode>();
+                    nodes = new List<ISymbolDefinitionNode>();
+                    _offsetToDefName[n.Offset] = nodes;
                 }
 
-                _offsetToDefName[n.Offset].Add(n);
+                nodes.Add(n);
                 _byteInterruptionOffsets[n.Offset] = true;
             }
 
index bde4ad0..d10255a 100644 (file)
@@ -524,9 +524,8 @@ namespace Microsoft.Extensions.Logging.Generators
                             var methods = new Dictionary<string, int>(lc.Methods.Count);
                             foreach (LoggerMethod lm in lc.Methods)
                             {
-                                if (methods.ContainsKey(lm.Name))
+                                if (methods.TryGetValue(lm.Name, out int currentCount))
                                 {
-                                    int currentCount = methods[lm.Name];
                                     lm.UniqueName = $"{lm.Name}{currentCount}";
                                     methods[lm.Name] = currentCount + 1;
                                 }
index 5143dca..d10573f 100644 (file)
@@ -139,11 +139,11 @@ namespace System.DirectoryServices.AccountManagement
                 // to build a list of ldap attributes for each object type.
                 if (null != ldapAttribute)
                 {
-                    if (propertyNameToLdapAttr.ContainsKey(propertyName))
+                    if (propertyNameToLdapAttr.TryGetValue(propertyName, out string[] ldapAttributes))
                     {
-                        string[] props = new string[propertyNameToLdapAttr[propertyName].Length + 1];
-                        propertyNameToLdapAttr[propertyName].CopyTo(props, 0);
-                        props[propertyNameToLdapAttr[propertyName].Length] = ldapAttribute;
+                        string[] props = new string[ldapAttributes.Length + 1];
+                        ldapAttributes.CopyTo(props, 0);
+                        props[ldapAttributes.Length] = ldapAttribute;
                         propertyNameToLdapAttr[propertyName] = props;
                     }
                     else
index 20b80b6..81c62a7 100644 (file)
@@ -343,9 +343,9 @@ namespace System.IO.Packaging
         {
             _package.ThrowIfWriteOnly();
 
-            if (!_propertyDictionary.ContainsKey(propertyName))
+            if (!_propertyDictionary.TryGetValue(propertyName, out object? value))
                 return null;
-            return _propertyDictionary[propertyName];
+            return value;
         }
 
         // Shim function to adequately cast the result of GetPropertyValue.
index 3d55640..5131a7d 100644 (file)
@@ -429,14 +429,13 @@ namespace System.Xml.XPath
         {
             XPathNodeRef nodeRef;
 
-            if (_idValueMap == null || !_idValueMap.ContainsKey(id))
+            if (_idValueMap == null || !_idValueMap.TryGetValue(id, out nodeRef))
             {
                 pageElem = null;
                 return 0;
             }
 
             // Extract page and index from XPathNodeRef
-            nodeRef = _idValueMap[id];
             pageElem = nodeRef.Page;
             return nodeRef.Index;
         }
index 255a6e5..de6549d 100644 (file)
@@ -25,19 +25,13 @@ namespace System.Xml.Xsl.IlGen
         /// </summary>
         public int Add(T value)
         {
-            int id;
-
-            if (!_lookup.ContainsKey(value))
+            if (!_lookup.TryGetValue(value, out int id))
             {
                 // The value does not yet exist, so add it to the list
                 id = _list.Count;
                 _lookup.Add(value, id);
                 _list.Add(value);
             }
-            else
-            {
-                id = _lookup[value];
-            }
 
             return id;
         }
index 88bbf33..90bf666 100644 (file)
@@ -87,10 +87,8 @@ namespace System.Security.AccessControl
             {
                 privilegeLock.EnterReadLock();
 
-                if (luids.ContainsKey(privilege))
+                if (luids.TryGetValue(privilege, out luid))
                 {
-                    luid = luids[privilege];
-
                     privilegeLock.ExitReadLock();
                 }
                 else
@@ -134,9 +132,8 @@ namespace System.Security.AccessControl
 
                 if (privilegeLock.IsWriteLockHeld)
                 {
-                    if (!luids.ContainsKey(privilege))
+                    if (luids.TryAdd(privilege, luid))
                     {
-                        luids[privilege] = luid;
                         privileges[luid] = privilege;
                     }
 
index 1832e5c..7fe2e19 100644 (file)
@@ -233,11 +233,13 @@ namespace System.Speech.Internal.GrammarBuilding
                 // Go deeper if the number of children is greater the element to compare against.
                 if (current != null)
                 {
-                    if (!dict.ContainsKey(current.Count))
+                    if (!dict.TryGetValue(current.Count, out Collection<BuilderElements> builderElements))
                     {
-                        dict.Add(current.Count, new Collection<BuilderElements>());
+                        builderElements = new Collection<BuilderElements>();
+                        dict.Add(current.Count, builderElements);
                     }
-                    dict[current.Count].Add(current);
+
+                    builderElements.Add(current);
 
                     current.GetDictionaryElements(dict);
                 }
index aaa125d..308e880 100644 (file)
@@ -490,16 +490,12 @@ namespace System.Speech.Internal.SrgsCompiler
                     if (srcToState != null)
                     {
                         // - If not found, clone a new DestToState, add SrcToState.DestToState to SrcToDestHash, and add SrcToState to CloneStack.
-                        if (!SrcToDestHash.ContainsKey(srcToState))
+                        if (!SrcToDestHash.TryGetValue(srcToState, out destToState))
                         {
                             destToState = CreateNewState(srcToState.Rule);
                             SrcToDestHash.Add(srcToState, destToState);
                             CloneStack.Push(srcToState);
                         }
-                        else
-                        {
-                            destToState = SrcToDestHash[srcToState];
-                        }
                     }
 
                     // - Clone the transition from SrcFromState.SrcToState at DestFromState.DestToState
index 2aa9132..f158cbb 100644 (file)
@@ -177,12 +177,13 @@ internal sealed class IcallTableGenerator
             if (icall == null)
             {
                 string? methodSig = BuildSignature(method, className);
-                if (methodSig != null && icallClass.Icalls.ContainsKey(methodSig))
-                    icall = icallClass.Icalls[methodSig];
+                if (methodSig != null)
+                    icallClass.Icalls.TryGetValue(methodSig, out icall);
+
+                if (icall == null)
+                    // Registered at runtime
+                    continue;
             }
-            if (icall == null)
-                // Registered at runtime
-                continue;
 
             icall.Method = method;
             icall.TokenIndex = (int)method.MetadataToken & 0xffffff;