From 4658a96dda62c9fb787f112850beff42d61277dc Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Sun, 26 Jul 2020 21:38:22 +0100 Subject: [PATCH 1/3] Autofix RCS1166: Value type object is never equal to null --- src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs | 4 ++-- .../commands/utility/ImplicitRemotingCommands.cs | 2 +- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs | 2 +- .../FormatAndOutput/common/TableWriter.cs | 2 +- .../engine/remoting/client/RemoteRunspacePoolInternal.cs | 2 +- .../engine/remoting/fanin/WSManPluginFacade.cs | 2 +- .../engine/remoting/fanin/WSManTransportManager.cs | 2 +- src/System.Management.Automation/help/CabinetNativeApi.cs | 2 +- .../help/UpdatableHelpModuleInfo.cs | 2 +- src/System.Management.Automation/help/UpdatableHelpUri.cs | 2 +- src/System.Management.Automation/security/Authenticode.cs | 2 +- 11 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs index e535535d25e..0fffe9e6af2 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs @@ -462,7 +462,7 @@ private uint GetCounterInfoPlus(IntPtr hCounter, out UInt32 counterType, out UIn defaultScale = 0; timeBase = 0; - Debug.Assert(hCounter != null); + Debug.Assert(hCounter != default); IntPtr pBufferSize = new IntPtr(0); res = PdhGetCounterInfo(hCounter, false, ref pBufferSize, IntPtr.Zero); @@ -1190,7 +1190,7 @@ public uint ReadNextSet(out PerformanceCounterSampleSet nextSet, bool bSkipReadi UInt64 timeBase = 0; IntPtr hCounter = _consumerPathToHandleAndInstanceMap[path].hCounter; - Debug.Assert(hCounter != null); + Debug.Assert(hCounter != default); res = GetCounterInfoPlus(hCounter, out counterType, out defaultScale, out timeBase); if (res != 0) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs index 58d9d762ab7..797e20f67af 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs @@ -1915,7 +1915,7 @@ internal ImplicitRemotingCodeGenerator( InvocationInfo invocationInfo) { Dbg.Assert(remoteRunspaceInfo != null, "Caller should validate remoteRunspaceInfo != null"); - Dbg.Assert(moduleGuid != null, "Caller should validate moduleGuid != null"); + Dbg.Assert(moduleGuid != default, "Caller should validate moduleGuid != null"); Dbg.Assert(invocationInfo != null, "Caller should validate invocationInfo != null"); _remoteRunspaceInfo = remoteRunspaceInfo; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 39a32d77c22..2faf412ab3b 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -1206,7 +1206,7 @@ private void Dispose(bool isDisposingNotFinalizing) if (!_isDisposed) { #if !UNIX - Dbg.Assert(breakHandlerGcHandle != null, "break handler should be set"); + Dbg.Assert(breakHandlerGcHandle != default, "break handler should be set"); ConsoleControl.RemoveBreakHandler(); if (breakHandlerGcHandle.IsAllocated) { diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index e01be6f5b8c..fea4440c60c 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -204,7 +204,7 @@ internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOn int cols = _si.columnInfo.Length; Span currentAlignment = cols <= OutCommandInner.StackAllocThreshold ? stackalloc int[cols] : new int[cols]; - if (alignment == null) + if (alignment == default) { for (int i = 0; i < currentAlignment.Length; i++) { diff --git a/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs b/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs index 7c2aceec7d4..668072dffeb 100644 --- a/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs +++ b/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs @@ -111,7 +111,7 @@ internal RemoteRunspacePoolInternal(Guid instanceId, string name, bool isDisconn ConnectCommandInfo[] connectCommands, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable) : base(1, 1) { - if (instanceId == null) + if (instanceId == default) { throw PSTraceSource.NewArgumentNullException("RunspacePool Guid"); } diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs index ed5bf0a4a3e..b55b530a924 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs @@ -318,7 +318,7 @@ private void populateDelegates() private void CleanUpDelegates() { // Free GCHandles so that the memory they point to may be unpinned (garbage collected) - if (_pluginShellGCHandle != null) + if (_pluginShellGCHandle != default) { _pluginShellGCHandle.Free(); _pluginReleaseShellContextGCHandle.Free(); diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs index 1fad7ccb3e8..eeae6dc4f37 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs @@ -2280,7 +2280,7 @@ private static void OnRemoteSessionConnectCallback(IntPtr operationContext, } // process returned Xml - Dbg.Assert(data != null, "WSManConnectShell callback returned null data"); + Dbg.Assert(data != default, "WSManConnectShell callback returned null data"); WSManNativeApi.WSManConnectDataResult connectData = WSManNativeApi.WSManConnectDataResult.UnMarshal(data); if (connectData.data != null) { diff --git a/src/System.Management.Automation/help/CabinetNativeApi.cs b/src/System.Management.Automation/help/CabinetNativeApi.cs index 4b19861e473..e4192383b8f 100644 --- a/src/System.Management.Automation/help/CabinetNativeApi.cs +++ b/src/System.Management.Automation/help/CabinetNativeApi.cs @@ -152,7 +152,7 @@ private void populateDelegates() private void CleanUpDelegates() { // Free GCHandles so that the memory they point to may be unpinned (garbage collected) - if (_fdiAllocHandle != null) + if (_fdiAllocHandle != default) { _fdiAllocHandle.Free(); _fdiFreeHandle.Free(); diff --git a/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs b/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs index 9f0c32d679a..b6b5656b5f0 100644 --- a/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs +++ b/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs @@ -28,7 +28,7 @@ internal class UpdatableHelpModuleInfo internal UpdatableHelpModuleInfo(string name, Guid guid, string path, string uri) { Debug.Assert(!string.IsNullOrEmpty(name)); - Debug.Assert(guid != null); + Debug.Assert(guid != default); Debug.Assert(!string.IsNullOrEmpty(path)); Debug.Assert(!string.IsNullOrEmpty(uri)); diff --git a/src/System.Management.Automation/help/UpdatableHelpUri.cs b/src/System.Management.Automation/help/UpdatableHelpUri.cs index 28683a3e2a8..81eb03cfba3 100644 --- a/src/System.Management.Automation/help/UpdatableHelpUri.cs +++ b/src/System.Management.Automation/help/UpdatableHelpUri.cs @@ -21,7 +21,7 @@ internal class UpdatableHelpUri internal UpdatableHelpUri(string moduleName, Guid moduleGuid, CultureInfo culture, string resolvedUri) { Debug.Assert(!string.IsNullOrEmpty(moduleName)); - Debug.Assert(moduleGuid != null); + Debug.Assert(moduleGuid != default); Debug.Assert(!string.IsNullOrEmpty(resolvedUri)); ModuleName = moduleName; diff --git a/src/System.Management.Automation/security/Authenticode.cs b/src/System.Management.Automation/security/Authenticode.cs index cf5e559f830..2abc43ddf73 100644 --- a/src/System.Management.Automation/security/Authenticode.cs +++ b/src/System.Management.Automation/security/Authenticode.cs @@ -192,7 +192,7 @@ internal static Signature SignFile(SigningOption option, IntPtr.Zero); #pragma warning restore 56523 - if (si.pSignExtInfo != null) + if (si.pSignExtInfo != default) { Marshal.DestroyStructure(si.pSignExtInfo); Marshal.FreeCoTaskMem(si.pSignExtInfo); From c9c28b2d7b9ce829e80098bb4c8e4e6f8e8e092f Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Mon, 27 Jul 2020 10:58:59 +0100 Subject: [PATCH 2/3] Refactor default value checks --- src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs | 4 ++-- .../commands/utility/ImplicitRemotingCommands.cs | 2 +- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs | 2 +- .../FormatAndOutput/common/TableWriter.cs | 2 +- .../engine/remoting/client/RemoteRunspacePoolInternal.cs | 4 ++-- .../engine/remoting/fanin/WSManPluginFacade.cs | 2 +- .../engine/remoting/fanin/WSManTransportManager.cs | 2 +- src/System.Management.Automation/help/CabinetNativeApi.cs | 2 +- .../help/UpdatableHelpModuleInfo.cs | 2 +- src/System.Management.Automation/help/UpdatableHelpUri.cs | 2 +- src/System.Management.Automation/security/Authenticode.cs | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs index 0fffe9e6af2..130a7f41804 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs @@ -462,7 +462,7 @@ private uint GetCounterInfoPlus(IntPtr hCounter, out UInt32 counterType, out UIn defaultScale = 0; timeBase = 0; - Debug.Assert(hCounter != default); + Debug.Assert(hCounter != IntPtr.Zero); IntPtr pBufferSize = new IntPtr(0); res = PdhGetCounterInfo(hCounter, false, ref pBufferSize, IntPtr.Zero); @@ -1190,7 +1190,7 @@ public uint ReadNextSet(out PerformanceCounterSampleSet nextSet, bool bSkipReadi UInt64 timeBase = 0; IntPtr hCounter = _consumerPathToHandleAndInstanceMap[path].hCounter; - Debug.Assert(hCounter != default); + Debug.Assert(hCounter != IntPtr.Zero); res = GetCounterInfoPlus(hCounter, out counterType, out defaultScale, out timeBase); if (res != 0) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs index 797e20f67af..feeb0cd091b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs @@ -1915,7 +1915,7 @@ internal ImplicitRemotingCodeGenerator( InvocationInfo invocationInfo) { Dbg.Assert(remoteRunspaceInfo != null, "Caller should validate remoteRunspaceInfo != null"); - Dbg.Assert(moduleGuid != default, "Caller should validate moduleGuid != null"); + Dbg.Assert(moduleGuid != Guid.Empty, "Caller should validate moduleGuid is not empty"); Dbg.Assert(invocationInfo != null, "Caller should validate invocationInfo != null"); _remoteRunspaceInfo = remoteRunspaceInfo; diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 2faf412ab3b..cc0d7eab044 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -1206,7 +1206,7 @@ private void Dispose(bool isDisposingNotFinalizing) if (!_isDisposed) { #if !UNIX - Dbg.Assert(breakHandlerGcHandle != default, "break handler should be set"); + Dbg.Assert(breakHandlerGcHandle.IsAllocated, "break handler should be set"); ConsoleControl.RemoveBreakHandler(); if (breakHandlerGcHandle.IsAllocated) { diff --git a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs index fea4440c60c..54aaa9438c5 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/TableWriter.cs @@ -204,7 +204,7 @@ internal void GenerateRow(string[] values, LineOutput lo, bool multiLine, ReadOn int cols = _si.columnInfo.Length; Span currentAlignment = cols <= OutCommandInner.StackAllocThreshold ? stackalloc int[cols] : new int[cols]; - if (alignment == default) + if (alignment.IsEmpty) { for (int i = 0; i < currentAlignment.Length; i++) { diff --git a/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs b/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs index 668072dffeb..17b07b0f148 100644 --- a/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs +++ b/src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs @@ -111,9 +111,9 @@ internal RemoteRunspacePoolInternal(Guid instanceId, string name, bool isDisconn ConnectCommandInfo[] connectCommands, RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable) : base(1, 1) { - if (instanceId == default) + if (instanceId == Guid.Empty) { - throw PSTraceSource.NewArgumentNullException("RunspacePool Guid"); + throw PSTraceSource.NewArgumentException(nameof(instanceId)); } if (connectCommands == null) diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs index b55b530a924..31f0e0d268c 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManPluginFacade.cs @@ -318,7 +318,7 @@ private void populateDelegates() private void CleanUpDelegates() { // Free GCHandles so that the memory they point to may be unpinned (garbage collected) - if (_pluginShellGCHandle != default) + if (_pluginShellGCHandle.IsAllocated) { _pluginShellGCHandle.Free(); _pluginReleaseShellContextGCHandle.Free(); diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs index eeae6dc4f37..92cb98664e3 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManTransportManager.cs @@ -2280,7 +2280,7 @@ private static void OnRemoteSessionConnectCallback(IntPtr operationContext, } // process returned Xml - Dbg.Assert(data != default, "WSManConnectShell callback returned null data"); + Dbg.Assert(data != IntPtr.Zero, "WSManConnectShell callback returned null data"); WSManNativeApi.WSManConnectDataResult connectData = WSManNativeApi.WSManConnectDataResult.UnMarshal(data); if (connectData.data != null) { diff --git a/src/System.Management.Automation/help/CabinetNativeApi.cs b/src/System.Management.Automation/help/CabinetNativeApi.cs index e4192383b8f..833ed6506f0 100644 --- a/src/System.Management.Automation/help/CabinetNativeApi.cs +++ b/src/System.Management.Automation/help/CabinetNativeApi.cs @@ -152,7 +152,7 @@ private void populateDelegates() private void CleanUpDelegates() { // Free GCHandles so that the memory they point to may be unpinned (garbage collected) - if (_fdiAllocHandle != default) + if (_fdiAllocHandle.IsAllocated) { _fdiAllocHandle.Free(); _fdiFreeHandle.Free(); diff --git a/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs b/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs index b6b5656b5f0..6da6c256dc5 100644 --- a/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs +++ b/src/System.Management.Automation/help/UpdatableHelpModuleInfo.cs @@ -28,7 +28,7 @@ internal class UpdatableHelpModuleInfo internal UpdatableHelpModuleInfo(string name, Guid guid, string path, string uri) { Debug.Assert(!string.IsNullOrEmpty(name)); - Debug.Assert(guid != default); + Debug.Assert(guid != Guid.Empty); Debug.Assert(!string.IsNullOrEmpty(path)); Debug.Assert(!string.IsNullOrEmpty(uri)); diff --git a/src/System.Management.Automation/help/UpdatableHelpUri.cs b/src/System.Management.Automation/help/UpdatableHelpUri.cs index 81eb03cfba3..8b854e82a5d 100644 --- a/src/System.Management.Automation/help/UpdatableHelpUri.cs +++ b/src/System.Management.Automation/help/UpdatableHelpUri.cs @@ -21,7 +21,7 @@ internal class UpdatableHelpUri internal UpdatableHelpUri(string moduleName, Guid moduleGuid, CultureInfo culture, string resolvedUri) { Debug.Assert(!string.IsNullOrEmpty(moduleName)); - Debug.Assert(moduleGuid != default); + Debug.Assert(moduleGuid != Guid.Empty); Debug.Assert(!string.IsNullOrEmpty(resolvedUri)); ModuleName = moduleName; diff --git a/src/System.Management.Automation/security/Authenticode.cs b/src/System.Management.Automation/security/Authenticode.cs index 2abc43ddf73..0abdafa956c 100644 --- a/src/System.Management.Automation/security/Authenticode.cs +++ b/src/System.Management.Automation/security/Authenticode.cs @@ -192,7 +192,7 @@ internal static Signature SignFile(SigningOption option, IntPtr.Zero); #pragma warning restore 56523 - if (si.pSignExtInfo != default) + if (si.pSignExtInfo != IntPtr.Zero) { Marshal.DestroyStructure(si.pSignExtInfo); Marshal.FreeCoTaskMem(si.pSignExtInfo); From 7c6f6fce4c15d00ed43618ec5a9459e8061ddaab Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 31 Jul 2020 02:37:31 +0100 Subject: [PATCH 3/3] Remove assert --- src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index cc0d7eab044..206a7e44e7f 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -1206,7 +1206,6 @@ private void Dispose(bool isDisposingNotFinalizing) if (!_isDisposed) { #if !UNIX - Dbg.Assert(breakHandlerGcHandle.IsAllocated, "break handler should be set"); ConsoleControl.RemoveBreakHandler(); if (breakHandlerGcHandle.IsAllocated) {