Skip to content

Commit 7c62490

Browse files
authored
Use null propagation operator in remoting code (PowerShell#17790)
1 parent 2d4a6bf commit 7c62490

33 files changed

+106
-383
lines changed

src/System.Management.Automation/engine/remoting/client/ClientMethodExecutor.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ internal void Execute(PSDataCollectionStream<ErrorRecord> errorStream)
159159
{
160160
try
161161
{
162-
if (_clientHost.UI != null)
163-
{
164-
_clientHost.UI.WriteErrorLine(errorRecord.ToString());
165-
}
162+
_clientHost.UI?.WriteErrorLine(errorRecord.ToString());
166163
}
167164
catch (Exception)
168165
{

src/System.Management.Automation/engine/remoting/client/ClientRemotePowerShell.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ internal void UnblockCollections()
166166

167167
outputstream.Close();
168168
errorstream.Close();
169-
if (inputstream != null)
170-
{
171-
inputstream.Close();
172-
}
169+
inputstream?.Close();
173170
}
174171

175172
/// <summary>

src/System.Management.Automation/engine/remoting/client/Job.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,10 +1448,7 @@ internal void SetJobState(JobState state, Exception reason)
14481448
{
14491449
lock (syncObject)
14501450
{
1451-
if (_finished != null)
1452-
{
1453-
_finished.Set();
1454-
}
1451+
_finished?.Set();
14551452
}
14561453
}
14571454
#pragma warning restore 56500
@@ -4074,10 +4071,7 @@ public override void SetDebuggerStepMode(bool enabled)
40744071
internal void CheckStateAndRaiseStopEvent()
40754072
{
40764073
RemoteDebugger remoteDebugger = _wrappedDebugger as RemoteDebugger;
4077-
if (remoteDebugger != null)
4078-
{
4079-
remoteDebugger.CheckStateAndRaiseStopEvent();
4080-
}
4074+
remoteDebugger?.CheckStateAndRaiseStopEvent();
40814075
}
40824076

40834077
/// <summary>
@@ -4125,13 +4119,7 @@ private Pipeline DrainAndBlockRemoteOutput()
41254119
return null;
41264120
}
41274121

4128-
private static void RestoreRemoteOutput(Pipeline runningCmd)
4129-
{
4130-
if (runningCmd != null)
4131-
{
4132-
runningCmd.ResumeIncomingData();
4133-
}
4134-
}
4122+
private static void RestoreRemoteOutput(Pipeline runningCmd) => runningCmd?.ResumeIncomingData();
41354123

41364124
private void HandleBreakpointUpdated(object sender, BreakpointUpdatedEventArgs e)
41374125
{

src/System.Management.Automation/engine/remoting/client/Job2.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,11 +2021,8 @@ protected override void Dispose(bool disposing)
20212021
job.Dispose();
20222022
}
20232023

2024-
if (_jobRunning != null)
2025-
_jobRunning.Dispose();
2026-
2027-
if (_jobSuspendedOrAborted != null)
2028-
_jobSuspendedOrAborted.Dispose();
2024+
_jobRunning?.Dispose();
2025+
_jobSuspendedOrAborted?.Dispose();
20292026
}
20302027
finally
20312028
{

src/System.Management.Automation/engine/remoting/client/RemoteRunspacePoolInternal.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,20 +1826,14 @@ private void ResetDisconnectedOnExpiresOn()
18261826
{
18271827
// Reset DisconnectedOn/ExpiresOn
18281828
WSManConnectionInfo wsManConnectionInfo = _connectionInfo as WSManConnectionInfo;
1829-
if (wsManConnectionInfo != null)
1830-
{
1831-
wsManConnectionInfo.NullDisconnectedExpiresOn();
1832-
}
1829+
wsManConnectionInfo?.NullDisconnectedExpiresOn();
18331830
}
18341831

18351832
private void UpdateDisconnectedExpiresOn()
18361833
{
18371834
// Set DisconnectedOn/ExpiresOn for disconnected session.
18381835
WSManConnectionInfo wsManConnectionInfo = _connectionInfo as WSManConnectionInfo;
1839-
if (wsManConnectionInfo != null)
1840-
{
1841-
wsManConnectionInfo.SetDisconnectedExpiresOnToNow();
1842-
}
1836+
wsManConnectionInfo?.SetDisconnectedExpiresOnToNow();
18431837
}
18441838

18451839
/// <summary>

src/System.Management.Automation/engine/remoting/client/RemotingProtocol2.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,7 @@ internal void DispatchMessageToPowerShell(RemoteDataObject<PSObject> rcvdData)
275275
// if a data structure handler does not exist it means
276276
// the association has been removed -
277277
// discard messages
278-
if (dsHandler != null)
279-
{
280-
dsHandler.ProcessReceivedData(rcvdData);
281-
}
278+
dsHandler?.ProcessReceivedData(rcvdData);
282279
}
283280

284281
/// <summary>

src/System.Management.Automation/engine/remoting/client/ThrottlingJob.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ protected override void Dispose(bool disposing)
5353
childJob.Dispose();
5454
}
5555

56-
if (_jobResultsThrottlingSemaphore != null)
57-
{
58-
_jobResultsThrottlingSemaphore.Dispose();
59-
}
60-
56+
_jobResultsThrottlingSemaphore?.Dispose();
6157
_cancellationTokenSource.Dispose();
6258
}
6359
}
@@ -541,10 +537,7 @@ private void StartChildJobIfPossible()
541537
} while (false);
542538
}
543539

544-
if (readyToRunChildJob != null)
545-
{
546-
readyToRunChildJob.StartJob();
547-
}
540+
readyToRunChildJob?.StartJob();
548541
}
549542

550543
private void EnqueueReadyToRunChildJob(StartableJob childJob)
@@ -1227,10 +1220,7 @@ public static void ForwardAllResultsToCmdlet(ThrottlingJob throttlingJob, Cmdlet
12271220
}
12281221
finally
12291222
{
1230-
if (cancellationTokenRegistration != null)
1231-
{
1232-
cancellationTokenRegistration.Dispose();
1233-
}
1223+
cancellationTokenRegistration?.Dispose();
12341224
}
12351225
}
12361226
finally

src/System.Management.Automation/engine/remoting/client/clientremotesessionprotocolstatemachine.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,7 @@ private void SetStateHandler(object sender, RemoteSessionStateMachineEventArgs e
254254
if (_state == RemoteSessionState.EstablishedAndKeySent)
255255
{
256256
Timer tmp = Interlocked.Exchange(ref _keyExchangeTimer, null);
257-
if (tmp != null)
258-
{
259-
tmp.Dispose();
260-
}
257+
tmp?.Dispose();
261258

262259
_keyExchanged = true;
263260
SetState(RemoteSessionState.Established, eventArgs.Reason);
@@ -339,10 +336,7 @@ private void HandleKeyExchangeTimeout(object sender)
339336
Dbg.Assert(_state == RemoteSessionState.EstablishedAndKeySent, "timeout should only happen when waiting for a key");
340337

341338
Timer tmp = Interlocked.Exchange(ref _keyExchangeTimer, null);
342-
if (tmp != null)
343-
{
344-
tmp.Dispose();
345-
}
339+
tmp?.Dispose();
346340

347341
PSRemotingDataStructureException exception =
348342
new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientKeyExchangeFailed);

src/System.Management.Automation/engine/remoting/client/remoterunspace.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,8 @@ protected override void Dispose(bool disposing)
634634
//
635635
}
636636

637-
if (_remoteDebugger != null)
638-
{
639-
// Release RunspacePool event forwarding handlers.
640-
_remoteDebugger.Dispose();
641-
}
637+
// Release RunspacePool event forwarding handlers.
638+
_remoteDebugger?.Dispose();
642639

643640
try
644641
{
@@ -1732,10 +1729,7 @@ internal void AbortOpen()
17321729
System.Management.Automation.Remoting.Client.NamedPipeClientSessionTransportManager transportManager =
17331730
RunspacePool.RemoteRunspacePoolInternal.DataStructureHandler.TransportManager as System.Management.Automation.Remoting.Client.NamedPipeClientSessionTransportManager;
17341731

1735-
if (transportManager != null)
1736-
{
1737-
transportManager.AbortConnect();
1738-
}
1732+
transportManager?.AbortConnect();
17391733
}
17401734

17411735
#endregion Internal Methods

src/System.Management.Automation/engine/remoting/client/remoterunspaceinfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,7 @@ public static PSSession Create(
384384
_transportName = transportName
385385
};
386386

387-
if (psCmdlet != null)
388-
{
389-
psCmdlet.RunspaceRepository.Add(psSession);
390-
}
387+
psCmdlet?.RunspaceRepository.Add(psSession);
391388

392389
return psSession;
393390
}

0 commit comments

Comments
 (0)