From ebfb62b5eca8e4b1cb8bcf8d3fca46862a9e47dd Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Mon, 1 Aug 2022 18:21:42 +0200 Subject: [PATCH] Use null propagation in the `Utility` module (#17787) --- .../commands/utility/CsvCommands.cs | 5 +---- .../commands/utility/CustomSerialization.cs | 5 +---- .../commands/utility/DebugRunspaceCommand.cs | 10 +++------- .../commands/utility/ExportAliasCommand.cs | 6 ++---- .../FormatAndOutput/OutGridView/OutWindowProxy.cs | 8 +------- .../commands/utility/ShowMarkdownCommand.cs | 5 +---- .../commands/utility/StartSleepCommand.cs | 10 ++-------- .../commands/utility/Update-TypeData.cs | 5 +---- .../WebCmdlet/Common/WebRequestPSCmdlet.Common.cs | 13 ++----------- .../commands/utility/XmlCommands.cs | 14 +++----------- .../utility/trace/TraceExpressionCommand.cs | 8 +------- 11 files changed, 18 insertions(+), 71 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs index 19ac076dea..eb3ebacff4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs @@ -429,10 +429,7 @@ namespace Microsoft.PowerShell.Commands _readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly; } - if (_helper != null) - { - _helper.Dispose(); - } + _helper?.Dispose(); } private void ReconcilePreexistingPropertyNames() diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs index 6ad8b02a55..5f810201e7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs @@ -170,10 +170,7 @@ namespace System.Management.Automation internal void Stop() { CustomInternalSerializer serializer = _serializer; - if (serializer != null) - { - serializer.Stop(); - } + serializer?.Stop(); } #endregion diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/DebugRunspaceCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/DebugRunspaceCommand.cs index f6ba764662..fc257810d5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/DebugRunspaceCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/DebugRunspaceCommand.cs @@ -236,10 +236,7 @@ namespace Microsoft.PowerShell.Commands // Unblock the data collection. PSDataCollection debugCollection = _debugBlockingCollection; - if (debugCollection != null) - { - debugCollection.Complete(); - } + debugCollection?.Complete(); // Unblock any new command wait. _newRunningScriptEvent.Set(); @@ -334,9 +331,8 @@ namespace Microsoft.PowerShell.Commands private void AddDataEventHandlers() { // Create new collection objects. - if (_debugBlockingCollection != null) { _debugBlockingCollection.Dispose(); } - - if (_debugAccumulateCollection != null) { _debugAccumulateCollection.Dispose(); } + _debugBlockingCollection?.Dispose(); + _debugAccumulateCollection?.Dispose(); _debugBlockingCollection = new PSDataCollection(); _debugBlockingCollection.BlockingEnumerator = true; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ExportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ExportAliasCommand.cs index e01d8c28e0..fcf282d09c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ExportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ExportAliasCommand.cs @@ -291,8 +291,7 @@ namespace Microsoft.PowerShell.Commands line = GetAliasLine(alias, "set-alias -Name:\"{0}\" -Value:\"{1}\" -Description:\"{2}\" -Option:\"{3}\""); } - if (writer != null) - writer.WriteLine(line); + writer?.WriteLine(line); if (PassThru) { @@ -302,8 +301,7 @@ namespace Microsoft.PowerShell.Commands } finally { - if (writer != null) - writer.Dispose(); + writer?.Dispose(); // reset the read-only attribute if (readOnlyFileInfo != null) readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutWindowProxy.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutWindowProxy.cs index 5ca41608a7..c7f2c622c0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutWindowProxy.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutWindowProxy.cs @@ -230,13 +230,7 @@ namespace Microsoft.PowerShell.Commands } } - internal void BlockUntilClosed() - { - if (_closedEvent != null) - { - _closedEvent.WaitOne(); - } - } + internal void BlockUntilClosed() => _closedEvent?.WaitOne(); /// /// Implements IDisposable logic. diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowMarkdownCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowMarkdownCommand.cs index 63ee15f6f5..3f40ec3439 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowMarkdownCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowMarkdownCommand.cs @@ -224,10 +224,7 @@ namespace Microsoft.PowerShell.Commands /// protected override void EndProcessing() { - if (_powerShell != null) - { - _powerShell.Dispose(); - } + _powerShell?.Dispose(); } } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/StartSleepCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/StartSleepCommand.cs index a1524e5ed4..3684981437 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/StartSleepCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/StartSleepCommand.cs @@ -91,10 +91,7 @@ namespace Microsoft.PowerShell.Commands } } - if (_waitHandle != null) - { - _waitHandle.WaitOne(milliSecondsToSleep, true); - } + _waitHandle?.WaitOne(milliSecondsToSleep, true); } /// @@ -150,10 +147,7 @@ namespace Microsoft.PowerShell.Commands lock (_syncObject) { _stopping = true; - if (_waitHandle != null) - { - _waitHandle.Set(); - } + _waitHandle?.Set(); } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs index cc8c7cebbc..8e07a80384 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs @@ -1160,10 +1160,7 @@ namespace Microsoft.PowerShell.Commands indicesToRemove.Sort(); for (int i = indicesToRemove.Count - 1; i >= 0; i--) { - if (Context.InitialSessionState != null) - { - Context.InitialSessionState.Types.RemoveItem(indicesToRemove[i]); - } + Context.InitialSessionState?.Types.RemoveItem(indicesToRemove[i]); } try diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index dbea8d20df..692c895d21 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -1578,10 +1578,7 @@ namespace Microsoft.PowerShell.Commands } finally { - if (reader != null) - { - reader.Dispose(); - } + reader?.Dispose(); } if (!string.IsNullOrEmpty(detailMsg)) @@ -1657,13 +1654,7 @@ namespace Microsoft.PowerShell.Commands /// /// Implementing ^C, after start the BeginGetResponse. /// - protected override void StopProcessing() - { - if (_cancelToken != null) - { - _cancelToken.Cancel(); - } - } + protected override void StopProcessing() => _cancelToken?.Cancel(); #endregion Overrides diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs index 8db56196a6..74a8ee1b7c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs @@ -453,8 +453,7 @@ namespace Microsoft.PowerShell.Commands { CreateMemoryStream(); - if (_serializer != null) - _serializer.SerializeAsStream(InputObject); + _serializer?.SerializeAsStream(InputObject); if (_serializer != null) { @@ -472,8 +471,7 @@ namespace Microsoft.PowerShell.Commands } else { - if (_serializer != null) - _serializer.Serialize(InputObject); + _serializer?.Serialize(InputObject); } } @@ -801,13 +799,7 @@ namespace Microsoft.PowerShell.Commands } } - internal void Stop() - { - if (_deserializer != null) - { - _deserializer.Stop(); - } - } + internal void Stop() => _deserializer?.Stop(); } #region Select-Xml diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/trace/TraceExpressionCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/trace/TraceExpressionCommand.cs index 412f4a2d15..7ae437525d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/trace/TraceExpressionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/trace/TraceExpressionCommand.cs @@ -249,13 +249,7 @@ namespace Microsoft.PowerShell.Commands /// /// Ensures that the sub-pipeline we created gets stopped as well. /// - protected override void StopProcessing() - { - if (_pipeline != null) - { - _pipeline.Stop(); - } - } + protected override void StopProcessing() => _pipeline?.Stop(); #endregion Cmdlet code