Skip to content

Commit e8c66a3

Browse files
authored
Use null-coalescing assignment in /remoting and /hostifaces (PowerShell#17728)
1 parent f1595c4 commit e8c66a3

21 files changed

+44
-126
lines changed

src/System.Management.Automation/engine/hostifaces/AsyncResult.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public WaitHandle AsyncWaitHandle
8585
{
8686
lock (SyncObject)
8787
{
88-
if (_completedWaitHandle == null)
89-
{
90-
_completedWaitHandle = new ManualResetEvent(IsCompleted);
91-
}
88+
_completedWaitHandle ??= new ManualResetEvent(IsCompleted);
9289
}
9390
}
9491

src/System.Management.Automation/engine/hostifaces/LocalConnection.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ public override PSPrimitiveDictionary GetApplicationPrivateData()
8484
{
8585
lock (this.SyncRoot)
8686
{
87-
if (_applicationPrivateData == null)
88-
{
89-
_applicationPrivateData = new PSPrimitiveDictionary();
90-
}
87+
_applicationPrivateData ??= new PSPrimitiveDictionary();
9188
}
9289
}
9390

@@ -768,10 +765,7 @@ internal void LogEngineHealthEvent(Exception exception,
768765
/// </remarks>
769766
internal PipelineThread GetPipelineThread()
770767
{
771-
if (_pipelineThread == null)
772-
{
773-
_pipelineThread = new PipelineThread(this.ApartmentState);
774-
}
768+
_pipelineThread ??= new PipelineThread(this.ApartmentState);
775769

776770
return _pipelineThread;
777771
}

src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,7 @@ internal static TranscriptionOption GetSystemTranscriptOption(TranscriptionOptio
10901090
// This way, multiple runspaces opened by the same process will share the same transcript.
10911091
lock (s_systemTranscriptLock)
10921092
{
1093-
if (systemTranscript == null)
1094-
{
1095-
systemTranscript = PSHostUserInterface.GetTranscriptOptionFromSettings(transcription, currentTranscript);
1096-
}
1093+
systemTranscript ??= PSHostUserInterface.GetTranscriptOptionFromSettings(transcription, currentTranscript);
10971094
}
10981095
}
10991096

src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,12 +1322,9 @@ internal WaitHandle WaitHandle
13221322
{
13231323
lock (SyncObject)
13241324
{
1325-
if (_readWaitHandle == null)
1326-
{
1327-
// Create the handle signaled if there are objects in the buffer
1328-
// or the buffer has been closed.
1329-
_readWaitHandle = new ManualResetEvent(_data.Count > 0 || !_isOpen);
1330-
}
1325+
// Create the handle signaled if there are objects in the buffer
1326+
// or the buffer has been closed.
1327+
_readWaitHandle ??= new ManualResetEvent(_data.Count > 0 || !_isOpen);
13311328
}
13321329
}
13331330

src/System.Management.Automation/engine/hostifaces/PSTask.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,12 +1536,9 @@ public Debugger Debugger
15361536
{
15371537
get
15381538
{
1539-
if (_jobDebuggerWrapper == null)
1540-
{
1541-
_jobDebuggerWrapper = new PSTaskChildDebugger(
1542-
_task.Debugger,
1543-
this.Name);
1544-
}
1539+
_jobDebuggerWrapper ??= new PSTaskChildDebugger(
1540+
_task.Debugger,
1541+
this.Name);
15451542

15461543
return _jobDebuggerWrapper;
15471544
}

src/System.Management.Automation/engine/hostifaces/PowerShell.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,7 @@ internal void InitForRemotePipeline(CommandCollection command, ObjectStreamBase
762762

763763
// create the client remote powershell for remoting
764764
// communications
765-
if (RemotePowerShell == null)
766-
{
767-
RemotePowerShell = new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
768-
}
765+
RemotePowerShell ??= new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
769766

770767
// If we get here, we don't call 'Invoke' or any of it's friends on 'this', instead we serialize 'this' in PowerShell.ToPSObjectForRemoting.
771768
// Without the following two steps, we'll be missing the 'ExtraCommands' on the serialized instance of 'this'.
@@ -804,10 +801,7 @@ internal void InitForRemotePipelineConnect(ObjectStreamBase inputstream, ObjectS
804801

805802
RedirectShellErrorOutputPipe = redirectShellErrorOutputPipe;
806803

807-
if (RemotePowerShell == null)
808-
{
809-
RemotePowerShell = new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
810-
}
804+
RemotePowerShell ??= new ClientRemotePowerShell(this, ((RunspacePool)_rsConnection).RemoteRunspacePoolInternal);
811805

812806
if (!RemotePowerShell.Initialized)
813807
{
@@ -2236,10 +2230,7 @@ internal void InvokeWithDebugger(
22362230
{
22372231
if (addToHistory)
22382232
{
2239-
if (settings == null)
2240-
{
2241-
settings = new PSInvocationSettings();
2242-
}
2233+
settings ??= new PSInvocationSettings();
22432234

22442235
settings.AddToHistory = true;
22452236
}
@@ -3544,10 +3535,7 @@ private void BatchInvocationCallback(IAsyncResult result)
35443535
break;
35453536
}
35463537

3547-
if (objs == null)
3548-
{
3549-
objs = _batchAsyncResult.Output;
3550-
}
3538+
objs ??= _batchAsyncResult.Output;
35513539

35523540
DoRemainingBatchCommands(objs);
35533541
}

src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,7 @@ internal virtual PSPrimitiveDictionary GetApplicationPrivateData()
231231
{
232232
lock (this.syncObject)
233233
{
234-
if (_applicationPrivateData == null)
235-
{
236-
_applicationPrivateData = new PSPrimitiveDictionary();
237-
}
234+
_applicationPrivateData ??= new PSPrimitiveDictionary();
238235
}
239236
}
240237

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,7 @@ public IList<Job> ChildJobs
633633
{
634634
lock (syncObject)
635635
{
636-
if (_childJobs == null)
637-
{
638-
_childJobs = new List<Job>();
639-
}
636+
_childJobs ??= new List<Job>();
640637
}
641638
}
642639

@@ -3371,13 +3368,10 @@ protected void ProcessJobFailure(ExecutionCmdletHelper helper, out Exception fai
33713368
}
33723369
}
33733370

3374-
if (failureException == null)
3375-
{
3376-
failureException = new RuntimeException(
3377-
PSRemotingErrorInvariants.FormatResourceString(
3378-
RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState,
3379-
runspace.RunspaceStateInfo.State));
3380-
}
3371+
failureException ??= new RuntimeException(
3372+
PSRemotingErrorInvariants.FormatResourceString(
3373+
RemotingErrorIdStrings.RemoteRunspaceOpenUnknownState,
3374+
runspace.RunspaceStateInfo.State));
33813375

33823376
failureErrorRecord = new ErrorRecord(failureException, targetObject,
33833377
fullyQualifiedErrorId, ErrorCategory.OpenError,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ public List<CommandParameterCollection> StartParameters
9393
{
9494
lock (_syncobject)
9595
{
96-
if (_parameters == null)
97-
_parameters = new List<CommandParameterCollection>();
96+
_parameters ??= new List<CommandParameterCollection>();
9897
}
9998
}
10099

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,9 @@ internal Pipeline CreatePipeline(string line, bool addToHistory, bool useNestedP
208208
}
209209

210210
// If that didn't work out fall-back to the traditional approach.
211-
if (pipeline == null)
212-
{
213-
pipeline = useNestedPipelines ?
214-
_runspaceRef.Value.CreateNestedPipeline(line, addToHistory) :
215-
_runspaceRef.Value.CreatePipeline(line, addToHistory);
216-
}
211+
pipeline ??= useNestedPipelines ?
212+
_runspaceRef.Value.CreateNestedPipeline(line, addToHistory) :
213+
_runspaceRef.Value.CreatePipeline(line, addToHistory);
217214

218215
// Add robust connection callback if this is a pushed runspace.
219216
RemotePipeline remotePipeline = pipeline as RemotePipeline;

0 commit comments

Comments
 (0)