Fix new warnings found with CA1854 improvement. (#85613)
authorBuyaa Namnan <bunamnan@microsoft.com>
Tue, 2 May 2023 04:25:00 +0000 (21:25 -0700)
committerGitHub <noreply@github.com>
Tue, 2 May 2023 04:25:00 +0000 (21:25 -0700)
* Fix new warnings found with CA1854 improvement

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs
src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpEndPointManager.cs
src/tools/illink/src/linker/Linker.Dataflow/MethodBodyScanner.cs

index 397a8dc..7c9bf4d 100644 (file)
@@ -254,10 +254,10 @@ namespace {lc.Namespace}
                 {
                     // this is related to https://github.com/serilog/serilog-extensions-logging/issues/197
                     string name = p.CodeName;
-                    if (lm.TemplateMap.ContainsKey(name))
+                    if (lm.TemplateMap.TryGetValue(name, out string value))
                     {
                         // take the letter casing from the template
-                        name = lm.TemplateMap[name];
+                        name = value;
                     }
 
                     _builder.AppendLine($"                    {nestedIndentation}{index++} => new global::System.Collections.Generic.KeyValuePair<string, object?>(\"{name}\", this.{NormalizeSpecialSymbol(p.CodeName)}),");
index 430d626..44e1d94 100644 (file)
@@ -135,9 +135,9 @@ namespace System.Net
             }
 
             Dictionary<int, HttpEndPointListener>? p;
-            if (s_ipEndPoints.ContainsKey(addr))
+            if (s_ipEndPoints.TryGetValue(addr, out Dictionary<int, HttpEndPointListener>? value))
             {
-                p = s_ipEndPoints[addr];
+                p = value;
             }
             else
             {
@@ -146,9 +146,9 @@ namespace System.Net
             }
 
             HttpEndPointListener? epl;
-            if (p.ContainsKey(port))
+            if (p.TryGetValue(port, out HttpEndPointListener? epListener))
             {
-                epl = p[port];
+                epl = epListener;
             }
             else
             {
index 5b946d1..fe140b8 100644 (file)
@@ -138,8 +138,8 @@ namespace Mono.Linker.Dataflow
                                return;
                        }
 
-                       if (knownStacks.ContainsKey (newOffset)) {
-                               knownStacks[newOffset] = MergeStack (knownStacks[newOffset], newStack);
+                       if (knownStacks.TryGetValue (newOffset, out Stack<StackSlot>? value)) {
+                               knownStacks[newOffset] = MergeStack (value, newStack);
                        } else {
                                knownStacks.Add (newOffset, new Stack<StackSlot> (newStack.Reverse ()));
                        }
@@ -292,12 +292,12 @@ namespace Mono.Linker.Dataflow
                        foreach (Instruction operation in methodIL.Instructions) {
                                int curBasicBlock = blockIterator.MoveNext (operation);
 
-                               if (knownStacks.ContainsKey (operation.Offset)) {
+                               if (knownStacks.TryGetValue (operation.Offset, out Stack<StackSlot>? knownValue)) {
                                        if (currentStack == null) {
                                                // The stack copy constructor reverses the stack
-                                               currentStack = new Stack<StackSlot> (knownStacks[operation.Offset].Reverse ());
+                                               currentStack = new Stack<StackSlot> (knownValue.Reverse ());
                                        } else {
-                                               currentStack = MergeStack (currentStack, knownStacks[operation.Offset]);
+                                               currentStack = MergeStack (currentStack, knownValue);
                                        }
                                }