Skip to content

Commit 2d4a6bf

Browse files
authored
Use null propagation operator in Management and ConsoleHost (PowerShell#17791)
1 parent d204d29 commit 2d4a6bf

File tree

7 files changed

+27
-108
lines changed

7 files changed

+27
-108
lines changed

src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/SessionBasedWrapper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,7 @@ public override void EndProcessing()
683683
public override void StopProcessing()
684684
{
685685
Job jobToStop = _parentJob;
686-
if (jobToStop != null)
687-
{
688-
jobToStop.StopJob();
689-
}
686+
jobToStop?.StopJob();
690687

691688
base.StopProcessing();
692689
}

src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimChildJobBase.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,7 @@ internal override void StartJob()
315315
this.ExceptionSafeWrapper(delegate
316316
{
317317
IObservable<T> observable = this.GetCimOperation();
318-
if (observable != null)
319-
{
320-
observable.Subscribe(this);
321-
}
318+
observable?.Subscribe(this);
322319
});
323320
});
324321
}
@@ -522,10 +519,7 @@ internal CimOperationOptions CreateOperationOptions()
522519
}
523520

524521
CimCustomOptionsDictionary jobSpecificCustomOptions = this.GetJobSpecificCustomOptions();
525-
if (jobSpecificCustomOptions != null)
526-
{
527-
jobSpecificCustomOptions.Apply(operationOptions, CimSensitiveValueConverter);
528-
}
522+
jobSpecificCustomOptions?.Apply(operationOptions, CimSensitiveValueConverter);
529523

530524
return operationOptions;
531525
}

src/Microsoft.PowerShell.Commands.Management/commands/management/ContentCommandBase.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,7 @@ internal void CloseContent(List<ContentHolder> contentHolders, bool disposing)
377377
{
378378
try
379379
{
380-
if (holder.Writer != null)
381-
{
382-
holder.Writer.Close();
383-
}
380+
holder.Writer?.Close();
384381
}
385382
catch (Exception e) // Catch-all OK. 3rd party callout
386383
{
@@ -414,10 +411,7 @@ internal void CloseContent(List<ContentHolder> contentHolders, bool disposing)
414411

415412
try
416413
{
417-
if (holder.Reader != null)
418-
{
419-
holder.Reader.Close();
420-
}
414+
holder.Reader?.Close();
421415
}
422416
catch (Exception e) // Catch-all OK. 3rd party callout
423417
{

src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,7 @@ private void myProcess_Exited(object sender, System.EventArgs e)
938938
{
939939
if (System.Threading.Interlocked.Decrement(ref _numberOfProcessesToWaitFor) == 0)
940940
{
941-
if (_waitHandle != null)
942-
{
943-
_waitHandle.Set();
944-
}
941+
_waitHandle?.Set();
945942
}
946943
}
947944

@@ -1040,13 +1037,8 @@ protected override void EndProcessing()
10401037
/// <summary>
10411038
/// StopProcessing.
10421039
/// </summary>
1043-
protected override void StopProcessing()
1044-
{
1045-
if (_waitHandle != null)
1046-
{
1047-
_waitHandle.Set();
1048-
}
1049-
}
1040+
protected override void StopProcessing() => _waitHandle?.Set();
1041+
10501042
#endregion Overrides
10511043

10521044
}
@@ -2079,13 +2071,7 @@ protected override void BeginProcessing()
20792071
/// <summary>
20802072
/// Implements ^c, after creating a process.
20812073
/// </summary>
2082-
protected override void StopProcessing()
2083-
{
2084-
if (_waithandle != null)
2085-
{
2086-
_waithandle.Set();
2087-
}
2088-
}
2074+
protected override void StopProcessing() => _waithandle?.Set();
20892075

20902076
#endregion
20912077

@@ -2116,13 +2102,7 @@ private void Dispose(bool isDisposing)
21162102
/// <summary>
21172103
/// When Process exits the wait handle is set.
21182104
/// </summary>
2119-
private void myProcess_Exited(object sender, System.EventArgs e)
2120-
{
2121-
if (_waithandle != null)
2122-
{
2123-
_waithandle.Set();
2124-
}
2125-
}
2105+
private void myProcess_Exited(object sender, System.EventArgs e) => _waithandle?.Set();
21262106

21272107
private string ResolveFilePath(string path)
21282108
{
@@ -2221,15 +2201,8 @@ private void StreamClosing()
22212201
{
22222202
Thread.Sleep(1000);
22232203

2224-
if (_outputWriter != null)
2225-
{
2226-
_outputWriter.Dispose();
2227-
}
2228-
2229-
if (_errorWriter != null)
2230-
{
2231-
_errorWriter.Dispose();
2232-
}
2204+
_outputWriter?.Dispose();
2205+
_errorWriter?.Dispose();
22332206
}
22342207

22352208
private void SetupInputOutputRedirection(Process p)

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ internal static int Start(
189189
if ((s_cpp.ServerMode && s_cpp.NamedPipeServerMode) || (s_cpp.ServerMode && s_cpp.SocketServerMode) || (s_cpp.NamedPipeServerMode && s_cpp.SocketServerMode))
190190
{
191191
s_tracer.TraceError("Conflicting server mode parameters, parameters must be used exclusively.");
192-
if (s_theConsoleHost != null)
193-
{
194-
s_theConsoleHost.ui.WriteErrorLine(ConsoleHostStrings.ConflictingServerModeParameters);
195-
}
192+
s_theConsoleHost?.ui.WriteErrorLine(ConsoleHostStrings.ConflictingServerModeParameters);
196193

197194
return ExitCodeBadCommandLineParameter;
198195
}
@@ -501,10 +498,7 @@ private static void HandleBreak()
501498
if (runspaceRef != null)
502499
{
503500
var runspace = runspaceRef.Runspace;
504-
if (runspace != null)
505-
{
506-
runspace.Close();
507-
}
501+
runspace?.Close();
508502
}
509503
}
510504
}
@@ -1266,15 +1260,8 @@ private void Dispose(bool isDisposingNotFinalizing)
12661260
StopTranscribing();
12671261
}
12681262

1269-
if (_outputSerializer != null)
1270-
{
1271-
_outputSerializer.End();
1272-
}
1273-
1274-
if (_errorSerializer != null)
1275-
{
1276-
_errorSerializer.End();
1277-
}
1263+
_outputSerializer?.End();
1264+
_errorSerializer?.End();
12781265

12791266
if (_runspaceRef != null)
12801267
{
@@ -2233,10 +2220,7 @@ private void OnExecutionSuspended(object sender, DebuggerStopEventArgs e)
22332220
// For remote debugging block data coming from the main (not-nested)
22342221
// running command.
22352222
baseLoop = InputLoop.GetNonNestedLoop();
2236-
if (baseLoop != null)
2237-
{
2238-
baseLoop.BlockCommandOutput();
2239-
}
2223+
baseLoop?.BlockCommandOutput();
22402224
}
22412225

22422226
//
@@ -2281,10 +2265,7 @@ private void OnExecutionSuspended(object sender, DebuggerStopEventArgs e)
22812265
finally
22822266
{
22832267
_debuggerStopEventArgs = null;
2284-
if (baseLoop != null)
2285-
{
2286-
baseLoop.ResumeCommandOutput();
2287-
}
2268+
baseLoop?.ResumeCommandOutput();
22882269
}
22892270
}
22902271

@@ -2670,10 +2651,7 @@ e is RemoteException ||
26702651
bht = _parent._breakHandlerThread;
26712652
}
26722653

2673-
if (bht != null)
2674-
{
2675-
bht.Join();
2676-
}
2654+
bht?.Join();
26772655

26782656
// Once the pipeline has been executed, we toss any outstanding progress data and
26792657
// take down the display.

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,14 @@ class ConsoleHostUserInterface : PSHostUserInterface
139139
void
140140
PreWrite()
141141
{
142-
if (_progPane != null)
143-
{
144-
_progPane.Hide();
145-
}
142+
_progPane?.Hide();
146143
}
147144

148145
private
149146
void
150147
PostWrite()
151148
{
152-
if (_progPane != null)
153-
{
154-
_progPane.Show();
155-
}
149+
_progPane?.Show();
156150
}
157151

158152
private
@@ -178,20 +172,14 @@ class ConsoleHostUserInterface : PSHostUserInterface
178172
void
179173
PreRead()
180174
{
181-
if (_progPane != null)
182-
{
183-
_progPane.Hide();
184-
}
175+
_progPane?.Hide();
185176
}
186177

187178
private
188179
void
189180
PostRead()
190181
{
191-
if (_progPane != null)
192-
{
193-
_progPane.Show();
194-
}
182+
_progPane?.Show();
195183
}
196184

197185
private

src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,9 @@ internal void BlockCommandOutput()
599599
internal void ResumeCommandOutput()
600600
{
601601
RemotePipeline remotePipeline = _pipeline as RemotePipeline;
602-
if (remotePipeline != null)
603-
{
604-
// Resumes data flow.
605-
remotePipeline.ResumeIncomingData();
606-
}
602+
603+
// Resumes data flow.
604+
remotePipeline?.ResumeIncomingData();
607605
}
608606

609607
/// <summary>
@@ -697,10 +695,7 @@ internal static void CancelCurrentExecutor()
697695
temp = s_currentExecutor;
698696
}
699697

700-
if (temp != null)
701-
{
702-
temp.Cancel();
703-
}
698+
temp?.Cancel();
704699
}
705700

706701
// These statics are threadsafe, as there can be only one instance of ConsoleHost in a process at a time, and access

0 commit comments

Comments
 (0)