Fix/disable nullable warnings treated as errors with newer SDKs installed (#3616)
authorMike McLaughlin <mikem@microsoft.com>
Thu, 26 Jan 2023 00:40:02 +0000 (16:40 -0800)
committerGitHub <noreply@github.com>
Thu, 26 Jan 2023 00:40:02 +0000 (16:40 -0800)
* Fix/disable nullable warnings treated as errors with newer SDKs installed

* Missed a test cs file

15 files changed:
src/Microsoft.Diagnostics.ExtensionCommands/DumpAsyncCommand.cs
src/SOS/SOS.UnitTests/SOS.cs
src/tests/DbgShim.UnitTests/DbgShimTests.cs
src/tests/Microsoft.Diagnostics.DebugServices.UnitTests/DebugServicesTests.cs
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterPipelineUnitTests.cs
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterTriggerTests.cs
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventLogsPipelineUnitTests.cs
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventTracePipelineUnitTests.cs
src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs
src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessEnvironmentTests.cs
src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessInfoTests.cs
src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs
src/tests/Microsoft.Diagnostics.NETCore.Client/ReversedServerTests.cs
src/tests/dotnet-counters/JSONExporterTests.cs
src/tests/dotnet-trace/ChildProcessTests.cs

index 8f1bb430e70d5c34d34ba3e97eaa1c7fb070afff..d09bf213fdae5a69e1561cd7e24195e1e4f40b75 100644 (file)
@@ -459,7 +459,7 @@ namespace Microsoft.Diagnostics.ExtensionCommands
             // <summary>Outputs a line of information for each instance field on the object.</summary>
             void RenderFields(IAddressableTypedEntity? obj, int depth)
             {
-                if (obj is not null)
+                if (obj?.Type is not null)
                 {
                     string depthTab = new string(' ', depth * TabWidth);
 
@@ -489,33 +489,36 @@ namespace Microsoft.Diagnostics.ExtensionCommands
             // <summary>Gets a printable description for the specified object.</summary>
             string Describe(ClrObject obj)
             {
-                // Default the description to the type name.
-                string description = obj.Type.Name;
-
-                if (IsStateMachineBox(obj.Type))
+                string description = string.Empty;
+                if (obj.Type?.Name is not null)
                 {
-                    // Remove the boilerplate box type from the name.
-                    int pos = description.IndexOf("StateMachineBox<", StringComparison.Ordinal);
-                    if (pos >= 0)
+                    // Default the description to the type name.
+                    description = obj.Type.Name;
+
+                    if (IsStateMachineBox(obj.Type))
                     {
-                        ReadOnlySpan<char> slice = description.AsSpan(pos + "StateMachineBox<".Length);
-                        slice = slice.Slice(0, slice.Length - 1); // remove trailing >
-                        description = slice.ToString();
+                        // Remove the boilerplate box type from the name.
+                        int pos = description.IndexOf("StateMachineBox<", StringComparison.Ordinal);
+                        if (pos >= 0)
+                        {
+                            ReadOnlySpan<char> slice = description.AsSpan(pos + "StateMachineBox<".Length);
+                            slice = slice.Slice(0, slice.Length - 1); // remove trailing >
+                            description = slice.ToString();
+                        }
                     }
-                }
-                else if (TryGetValidObjectField(obj, "m_action", out ClrObject taskDelegate))
-                {
-                    // If we can figure out what the task's delegate points to, append the method signature.
-                    if (TryGetMethodFromDelegate(runtime, taskDelegate, out ClrMethod? method))
+                    else if (TryGetValidObjectField(obj, "m_action", out ClrObject taskDelegate))
+                    {
+                        // If we can figure out what the task's delegate points to, append the method signature.
+                        if (TryGetMethodFromDelegate(runtime, taskDelegate, out ClrMethod? method))
+                        {
+                            description = $"{description} {{{method!.Signature}}}";
+                        }
+                    }
+                    else if (obj.Address != 0 && taskCompletionSentinel.Address == obj.Address)
                     {
-                        description = $"{description} {{{method!.Signature}}}";
+                        description = "TaskCompletionSentinel";
                     }
                 }
-                else if (obj.Address != 0 && taskCompletionSentinel.Address == obj.Address)
-                {
-                    description = "TaskCompletionSentinel";
-                }
-
                 return description;
             }
 
@@ -527,14 +530,17 @@ namespace Microsoft.Diagnostics.ExtensionCommands
                     return false;
                 }
 
-                if (MethodTableAddress is ulong mt && obj.Type.MethodTable != mt)
+                if (obj.Type is not null)
                 {
-                    return false;
-                }
+                    if (MethodTableAddress is ulong mt && obj.Type.MethodTable != mt)
+                    {
+                        return false;
+                    }
 
-                if (NameSubstring is not null && !obj.Type.Name.Contains(NameSubstring))
-                {
-                    return false;
+                    if (NameSubstring is not null && obj.Type.Name is not null && !obj.Type.Name.Contains(NameSubstring))
+                    {
+                        return false;
+                    }
                 }
 
                 return true;
@@ -655,37 +661,42 @@ namespace Microsoft.Diagnostics.ExtensionCommands
             // </remarks>
             void AddContinuation(ClrObject continuation, List<ClrObject> continuations)
             {
-                if (continuation.Type.Name.StartsWith("System.Collections.Generic.List<", StringComparison.Ordinal))
+                if (continuation.Type is not null)
                 {
-                    if (continuation.Type.GetFieldByName("_items") is ClrInstanceField itemsField)
+                    if (continuation.Type.Name is not null &&
+                        continuation.Type.Name.StartsWith("System.Collections.Generic.List<", StringComparison.Ordinal))
                     {
-                        ClrObject itemsObj = itemsField.ReadObject(continuation.Address, interior: false);
-                        if (!itemsObj.IsNull)
+                        if (continuation.Type.GetFieldByName("_items") is ClrInstanceField itemsField)
                         {
-                            ClrArray items = itemsObj.AsArray();
-                            if (items.Rank == 1)
+                            ClrObject itemsObj = itemsField.ReadObject(continuation.Address, interior: false);
+                            if (!itemsObj.IsNull)
                             {
-                                for (int i = 0; i < items.Length; i++)
+                                ClrArray items = itemsObj.AsArray();
+                                if (items.Rank == 1)
                                 {
-                                    if (items.GetObjectValue(i) is ClrObject { IsValid: true } c)
+                                    for (int i = 0; i < items.Length; i++)
                                     {
-                                        continuations.Add(ResolveContinuation(c));
+                                        if (items.GetObjectValue(i) is ClrObject { IsValid: true } c)
+                                        {
+                                            continuations.Add(ResolveContinuation(c));
+                                        }
                                     }
                                 }
                             }
                         }
                     }
-                }
-                else
-                {
-                    continuations.Add(continuation);
+                    else
+                    {
+                        continuations.Add(continuation);
+                    }
                 }
             }
 
             // <summary>Tries to get the object contents of a Task's continuations field</summary>
             bool TryGetContinuation(ClrObject obj, out ClrObject continuation)
             {
-                if (obj.Type.GetFieldByName("m_continuationObject") is ClrInstanceField continuationObjectField &&
+                if (obj.Type is not null &&
+                    obj.Type.GetFieldByName("m_continuationObject") is ClrInstanceField continuationObjectField &&
                     continuationObjectField.ReadObject(obj.Address, interior: false) is ClrObject { IsValid: true } continuationObject)
                 {
                     continuation = ResolveContinuation(continuationObject);
@@ -888,7 +899,7 @@ namespace Microsoft.Diagnostics.ExtensionCommands
         }
 
         /// <summary>Gets whether the specified type is an AsyncStateMachineBox{T}.</summary>
-        private static bool IsStateMachineBox(ClrType type)
+        private static bool IsStateMachineBox(ClrType? type)
         {
             // Ideally we would compare the metadata token and module for the generic template for the type,
             // but that information isn't fully available via ClrMd, nor can it currently find DebugFinalizableAsyncStateMachineBox
index 34fffd02ab742b5508d354a74ad41786d7850aed..a0ec5a83aa3880fb0dd14961464f7364c9c027ec 100644 (file)
@@ -14,6 +14,11 @@ using Xunit;
 using Xunit.Abstractions;
 using Xunit.Extensions;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
+[Collection("Windows Dump Generation")]
 public class SOS
 {
     public SOS(ITestOutputHelper output)
index d66536a9ca5dad7f573bef8073c9714f8151d0aa..925403a312f723b02ce16a5b7655ef060783ddf3 100644 (file)
@@ -20,6 +20,10 @@ using Xunit;
 using Xunit.Abstractions;
 using Xunit.Extensions;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics
 {
     public class DbgShimTests : IDisposable
index b612ffbf69ccca0c03b400cdabd12caa4f5e8a73..ca975fb9c9ea28acb9a5e85fcb9078adc042df46 100644 (file)
@@ -11,6 +11,10 @@ using Xunit;
 using Xunit.Abstractions;
 using Xunit.Extensions;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.DebugServices.UnitTests
 {
     public class DebugServicesTests : IDisposable
index bfb15c6b6b93a20279697ad1abc486eaeca690a4..c392582ce256b6a4430241dc657a5f1f6824fa51 100644 (file)
@@ -13,6 +13,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests
 {
     public class EventCounterPipelineUnitTests
index 551e53e4b28995973fe045d60c48284f4698576f..80f05d4911dd0fd73d959f0cbcd4ce2996d42b99 100644 (file)
@@ -10,7 +10,6 @@ using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.Globalization;
-using System.Runtime.InteropServices;
 using System.Threading;
 using System.Threading.Tasks;
 using Xunit;
@@ -18,6 +17,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests
 {
     public class EventCounterTriggerTests
index 4d91d2c29d324bb3b6d21d7e4fe91a84834e209d..505ba69d204c237ce82b5ed5f354519e7e93e30e 100644 (file)
@@ -18,6 +18,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests
 {
     public class EventLogsPipelineUnitTests
index 8c88286e282e684074e17866fa39eea61cba437f..dc750bd799c5d5ba3ffcecc7ddc114f316a0a365 100644 (file)
@@ -17,6 +17,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests
 {
     public class EventTracePipelineUnitTests
index 1a3c39212e35ce812aceeb5cba024318c786309c..6efc9739f361b1019d63a5f15fc484e1145f8cd4 100644 (file)
@@ -14,6 +14,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.NETCore.Client
 {
     public class EventPipeSessionTests
index d191b42e20e5aa4b5e110a1e45332266cf4f3025..746f8cd70ededceda8d132d842fff2d195d80d60 100644 (file)
@@ -10,6 +10,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.NETCore.Client
 {
     public class ProcessEnvironmentTests
index 2b986fabf8e06d472a3800cd2023fcf02edf94ff..d61819b2bcb9089ee9593095433ecf62b061da5a 100644 (file)
@@ -13,6 +13,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.NETCore.Client
 {
     public class GetProcessInfoTests
index ff77348124be0702e68a8b6cdc425ab632cde418..8a3185db25e0eba79c93c2daf0f2d4d61c119b23 100644 (file)
@@ -5,7 +5,6 @@
 using Microsoft.Diagnostics.TestHelpers;
 using System;
 using System.Collections.Generic;
-using System.Runtime.InteropServices;
 using System.Threading;
 using System.Threading.Tasks;
 using Xunit;
@@ -13,6 +12,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.NETCore.Client
 {
 
index 4576994396d6860912462ee148fa38bf2460b14a..595e140adf536b9f4299eb876faf9299fdfec4f1 100644 (file)
@@ -19,6 +19,10 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.NETCore.Client
 {
     public class ReversedServerTests
index d60197dd33f17b4987b2a0c4c5fe2459d94f69a9..6ffc1e92dea8d772b0e9571088066a8b59e5f200 100644 (file)
@@ -4,12 +4,13 @@
 
 using System;
 using System.IO;
-using System.Collections.Generic;
 using Xunit;
 using Microsoft.Diagnostics.Tools.Counters.Exporters;
 using Newtonsoft.Json;
 using Microsoft.Diagnostics.Tools.Counters;
 
+#pragma warning disable CA1507 // Use nameof to express symbol names
+
 namespace DotnetCounters.UnitTests
 {
     /// <summary>
index 35a003046f77b5669376d86777c499f06692f04d..216483914eaf78de6992d93451e9631789a3fd23 100644 (file)
@@ -12,9 +12,12 @@ using Xunit.Abstractions;
 using Xunit.Extensions;
 using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
 
+// Newer SDKs flag MemberData(nameof(Configurations)) with this error
+// Avoid unnecessary zero-length array allocations.  Use Array.Empty<object>() instead.
+#pragma warning disable CA1825 
+
 namespace Microsoft.Diagnostics.Tools.Trace
 {
-
     public class ChildProcessTests
     {
         public static IEnumerable<object[]> Configurations => TestRunner.Configurations;