diff --git a/src/Microsoft.Diagnostics.ExtensionCommands/DumpAsyncCommand.cs b/src/Microsoft.Diagnostics.ExtensionCommands/DumpAsyncCommand.cs index 8f1bb430e7..d09bf213fd 100644 --- a/src/Microsoft.Diagnostics.ExtensionCommands/DumpAsyncCommand.cs +++ b/src/Microsoft.Diagnostics.ExtensionCommands/DumpAsyncCommand.cs @@ -459,7 +459,7 @@ bool ShouldIncludeStack(AsyncObject obj) // Outputs a line of information for each instance field on the object. 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 @@ void RenderFields(IAddressableTypedEntity? obj, int depth) // Gets a printable description for the specified object. 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 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 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 @@ bool IncludeInOutput(ClrObject obj) 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 @@ Dictionary CollectObjects() // void AddContinuation(ClrObject continuation, List 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); + } } } // Tries to get the object contents of a Task's continuations field 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 @@ private void WriteCodeLink(ulong address) } /// Gets whether the specified type is an AsyncStateMachineBox{T}. - 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 diff --git a/src/SOS/SOS.UnitTests/SOS.cs b/src/SOS/SOS.UnitTests/SOS.cs index 34fffd02ab..a0ec5a83aa 100644 --- a/src/SOS/SOS.UnitTests/SOS.cs +++ b/src/SOS/SOS.UnitTests/SOS.cs @@ -14,6 +14,11 @@ using Xunit.Abstractions; using Xunit.Extensions; +// Newer SDKs flag MemberData(nameof(Configurations)) with this error +// Avoid unnecessary zero-length array allocations. Use Array.Empty() instead. +#pragma warning disable CA1825 + +[Collection("Windows Dump Generation")] public class SOS { public SOS(ITestOutputHelper output) diff --git a/src/tests/DbgShim.UnitTests/DbgShimTests.cs b/src/tests/DbgShim.UnitTests/DbgShimTests.cs index d66536a9ca..925403a312 100644 --- a/src/tests/DbgShim.UnitTests/DbgShimTests.cs +++ b/src/tests/DbgShim.UnitTests/DbgShimTests.cs @@ -20,6 +20,10 @@ using Xunit.Abstractions; using Xunit.Extensions; +// Newer SDKs flag MemberData(nameof(Configurations)) with this error +// Avoid unnecessary zero-length array allocations. Use Array.Empty() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics { public class DbgShimTests : IDisposable diff --git a/src/tests/Microsoft.Diagnostics.DebugServices.UnitTests/DebugServicesTests.cs b/src/tests/Microsoft.Diagnostics.DebugServices.UnitTests/DebugServicesTests.cs index b612ffbf69..ca975fb9c9 100644 --- a/src/tests/Microsoft.Diagnostics.DebugServices.UnitTests/DebugServicesTests.cs +++ b/src/tests/Microsoft.Diagnostics.DebugServices.UnitTests/DebugServicesTests.cs @@ -11,6 +11,10 @@ using Xunit.Abstractions; using Xunit.Extensions; +// Newer SDKs flag MemberData(nameof(Configurations)) with this error +// Avoid unnecessary zero-length array allocations. Use Array.Empty() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.DebugServices.UnitTests { public class DebugServicesTests : IDisposable diff --git a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterPipelineUnitTests.cs b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterPipelineUnitTests.cs index bfb15c6b6b..c392582ce2 100644 --- a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterPipelineUnitTests.cs +++ b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterPipelineUnitTests.cs @@ -13,6 +13,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests { public class EventCounterPipelineUnitTests diff --git a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterTriggerTests.cs b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterTriggerTests.cs index 551e53e4b2..80f05d4911 100644 --- a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterTriggerTests.cs +++ b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventCounterTriggerTests.cs @@ -10,7 +10,6 @@ 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.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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests { public class EventCounterTriggerTests diff --git a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventLogsPipelineUnitTests.cs b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventLogsPipelineUnitTests.cs index 4d91d2c29d..505ba69d20 100644 --- a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventLogsPipelineUnitTests.cs +++ b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventLogsPipelineUnitTests.cs @@ -18,6 +18,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests { public class EventLogsPipelineUnitTests diff --git a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventTracePipelineUnitTests.cs b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventTracePipelineUnitTests.cs index 8c88286e28..dc750bd799 100644 --- a/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventTracePipelineUnitTests.cs +++ b/src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/EventTracePipelineUnitTests.cs @@ -17,6 +17,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.Monitoring.EventPipe.UnitTests { public class EventTracePipelineUnitTests diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs index 1a3c39212e..6efc9739f3 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs @@ -14,6 +14,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.NETCore.Client { public class EventPipeSessionTests diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessEnvironmentTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessEnvironmentTests.cs index d191b42e20..746f8cd70e 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessEnvironmentTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessEnvironmentTests.cs @@ -10,6 +10,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.NETCore.Client { public class ProcessEnvironmentTests diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessInfoTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessInfoTests.cs index 2b986fabf8..d61819b2bc 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessInfoTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetProcessInfoTests.cs @@ -13,6 +13,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.NETCore.Client { public class GetProcessInfoTests diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs index ff77348124..8a3185db25 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs @@ -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.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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.NETCore.Client { diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/ReversedServerTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/ReversedServerTests.cs index 4576994396..595e140adf 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/ReversedServerTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/ReversedServerTests.cs @@ -19,6 +19,10 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.NETCore.Client { public class ReversedServerTests diff --git a/src/tests/dotnet-counters/JSONExporterTests.cs b/src/tests/dotnet-counters/JSONExporterTests.cs index d60197dd33..6ffc1e92de 100644 --- a/src/tests/dotnet-counters/JSONExporterTests.cs +++ b/src/tests/dotnet-counters/JSONExporterTests.cs @@ -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 { /// diff --git a/src/tests/dotnet-trace/ChildProcessTests.cs b/src/tests/dotnet-trace/ChildProcessTests.cs index 35a003046f..216483914e 100644 --- a/src/tests/dotnet-trace/ChildProcessTests.cs +++ b/src/tests/dotnet-trace/ChildProcessTests.cs @@ -12,9 +12,12 @@ 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() instead. +#pragma warning disable CA1825 + namespace Microsoft.Diagnostics.Tools.Trace { - public class ChildProcessTests { public static IEnumerable Configurations => TestRunner.Configurations;