From 2d4a6bf47530d4492af3c35525f7db3cde2af18f Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Mon, 1 Aug 2022 18:20:44 +0200 Subject: [PATCH] Use null propagation operator in `Management` and `ConsoleHost` (#17791) --- .../cmdletization/SessionBasedWrapper.cs | 5 +-- .../cmdletization/cim/cimChildJobBase.cs | 10 +---- .../commands/management/ContentCommandBase.cs | 10 +---- .../commands/management/Process.cs | 41 ++++--------------- .../host/msh/ConsoleHost.cs | 36 ++++------------ .../msh/ConsoleHostUserInterfaceProgress.cs | 20 ++------- .../host/msh/Executor.cs | 13 ++---- 7 files changed, 27 insertions(+), 108 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs index 0ec77a18d..cca50c4bb 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs @@ -683,10 +683,7 @@ namespace Microsoft.PowerShell.Cmdletization public override void StopProcessing() { Job jobToStop = _parentJob; - if (jobToStop != null) - { - jobToStop.StopJob(); - } + jobToStop?.StopJob(); base.StopProcessing(); } diff --git a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs index 7ef78d13a..8a973c7af 100644 --- a/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs +++ b/src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs @@ -315,10 +315,7 @@ namespace Microsoft.PowerShell.Cmdletization.Cim this.ExceptionSafeWrapper(delegate { IObservable observable = this.GetCimOperation(); - if (observable != null) - { - observable.Subscribe(this); - } + observable?.Subscribe(this); }); }); } @@ -522,10 +519,7 @@ namespace Microsoft.PowerShell.Cmdletization.Cim } CimCustomOptionsDictionary jobSpecificCustomOptions = this.GetJobSpecificCustomOptions(); - if (jobSpecificCustomOptions != null) - { - jobSpecificCustomOptions.Apply(operationOptions, CimSensitiveValueConverter); - } + jobSpecificCustomOptions?.Apply(operationOptions, CimSensitiveValueConverter); return operationOptions; } diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ContentCommandBase.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ContentCommandBase.cs index 0c990292b..87c7731ed 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ContentCommandBase.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ContentCommandBase.cs @@ -377,10 +377,7 @@ namespace Microsoft.PowerShell.Commands { try { - if (holder.Writer != null) - { - holder.Writer.Close(); - } + holder.Writer?.Close(); } catch (Exception e) // Catch-all OK. 3rd party callout { @@ -414,10 +411,7 @@ namespace Microsoft.PowerShell.Commands try { - if (holder.Reader != null) - { - holder.Reader.Close(); - } + holder.Reader?.Close(); } catch (Exception e) // Catch-all OK. 3rd party callout { diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index ef4f5b1a7..5aa97eaeb 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -938,10 +938,7 @@ namespace Microsoft.PowerShell.Commands { if (System.Threading.Interlocked.Decrement(ref _numberOfProcessesToWaitFor) == 0) { - if (_waitHandle != null) - { - _waitHandle.Set(); - } + _waitHandle?.Set(); } } @@ -1040,13 +1037,8 @@ namespace Microsoft.PowerShell.Commands /// /// StopProcessing. /// - protected override void StopProcessing() - { - if (_waitHandle != null) - { - _waitHandle.Set(); - } - } + protected override void StopProcessing() => _waitHandle?.Set(); + #endregion Overrides } @@ -2079,13 +2071,7 @@ namespace Microsoft.PowerShell.Commands /// /// Implements ^c, after creating a process. /// - protected override void StopProcessing() - { - if (_waithandle != null) - { - _waithandle.Set(); - } - } + protected override void StopProcessing() => _waithandle?.Set(); #endregion @@ -2116,13 +2102,7 @@ namespace Microsoft.PowerShell.Commands /// /// When Process exits the wait handle is set. /// - private void myProcess_Exited(object sender, System.EventArgs e) - { - if (_waithandle != null) - { - _waithandle.Set(); - } - } + private void myProcess_Exited(object sender, System.EventArgs e) => _waithandle?.Set(); private string ResolveFilePath(string path) { @@ -2221,15 +2201,8 @@ namespace Microsoft.PowerShell.Commands { Thread.Sleep(1000); - if (_outputWriter != null) - { - _outputWriter.Dispose(); - } - - if (_errorWriter != null) - { - _errorWriter.Dispose(); - } + _outputWriter?.Dispose(); + _errorWriter?.Dispose(); } private void SetupInputOutputRedirection(Process p) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 672bcfabb..0d647a2de 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -189,10 +189,7 @@ namespace Microsoft.PowerShell if ((s_cpp.ServerMode && s_cpp.NamedPipeServerMode) || (s_cpp.ServerMode && s_cpp.SocketServerMode) || (s_cpp.NamedPipeServerMode && s_cpp.SocketServerMode)) { s_tracer.TraceError("Conflicting server mode parameters, parameters must be used exclusively."); - if (s_theConsoleHost != null) - { - s_theConsoleHost.ui.WriteErrorLine(ConsoleHostStrings.ConflictingServerModeParameters); - } + s_theConsoleHost?.ui.WriteErrorLine(ConsoleHostStrings.ConflictingServerModeParameters); return ExitCodeBadCommandLineParameter; } @@ -501,10 +498,7 @@ namespace Microsoft.PowerShell if (runspaceRef != null) { var runspace = runspaceRef.Runspace; - if (runspace != null) - { - runspace.Close(); - } + runspace?.Close(); } } } @@ -1266,15 +1260,8 @@ namespace Microsoft.PowerShell StopTranscribing(); } - if (_outputSerializer != null) - { - _outputSerializer.End(); - } - - if (_errorSerializer != null) - { - _errorSerializer.End(); - } + _outputSerializer?.End(); + _errorSerializer?.End(); if (_runspaceRef != null) { @@ -2233,10 +2220,7 @@ namespace Microsoft.PowerShell // For remote debugging block data coming from the main (not-nested) // running command. baseLoop = InputLoop.GetNonNestedLoop(); - if (baseLoop != null) - { - baseLoop.BlockCommandOutput(); - } + baseLoop?.BlockCommandOutput(); } // @@ -2281,10 +2265,7 @@ namespace Microsoft.PowerShell finally { _debuggerStopEventArgs = null; - if (baseLoop != null) - { - baseLoop.ResumeCommandOutput(); - } + baseLoop?.ResumeCommandOutput(); } } @@ -2670,10 +2651,7 @@ namespace Microsoft.PowerShell bht = _parent._breakHandlerThread; } - if (bht != null) - { - bht.Join(); - } + bht?.Join(); // Once the pipeline has been executed, we toss any outstanding progress data and // take down the display. diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index a36bc096d..1c9d37ea9 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -139,20 +139,14 @@ namespace Microsoft.PowerShell void PreWrite() { - if (_progPane != null) - { - _progPane.Hide(); - } + _progPane?.Hide(); } private void PostWrite() { - if (_progPane != null) - { - _progPane.Show(); - } + _progPane?.Show(); } private @@ -178,20 +172,14 @@ namespace Microsoft.PowerShell void PreRead() { - if (_progPane != null) - { - _progPane.Hide(); - } + _progPane?.Hide(); } private void PostRead() { - if (_progPane != null) - { - _progPane.Show(); - } + _progPane?.Show(); } private diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs index 601d4baab..013281d22 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs @@ -599,11 +599,9 @@ namespace Microsoft.PowerShell internal void ResumeCommandOutput() { RemotePipeline remotePipeline = _pipeline as RemotePipeline; - if (remotePipeline != null) - { - // Resumes data flow. - remotePipeline.ResumeIncomingData(); - } + + // Resumes data flow. + remotePipeline?.ResumeIncomingData(); } /// @@ -697,10 +695,7 @@ namespace Microsoft.PowerShell temp = s_currentExecutor; } - if (temp != null) - { - temp.Cancel(); - } + temp?.Cancel(); } // These statics are threadsafe, as there can be only one instance of ConsoleHost in a process at a time, and access