Skip to content

Commit ebfb62b

Browse files
authored
Use null propagation in the Utility module (PowerShell#17787)
1 parent 7c62490 commit ebfb62b

File tree

11 files changed

+18
-71
lines changed

11 files changed

+18
-71
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,7 @@ private void CleanUp()
429429
_readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;
430430
}
431431

432-
if (_helper != null)
433-
{
434-
_helper.Dispose();
435-
}
432+
_helper?.Dispose();
436433
}
437434

438435
private void ReconcilePreexistingPropertyNames()

src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ internal void DoneAsStream()
170170
internal void Stop()
171171
{
172172
CustomInternalSerializer serializer = _serializer;
173-
if (serializer != null)
174-
{
175-
serializer.Stop();
176-
}
173+
serializer?.Stop();
177174
}
178175

179176
#endregion

src/Microsoft.PowerShell.Commands.Utility/commands/utility/DebugRunspaceCommand.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,7 @@ protected override void StopProcessing()
236236

237237
// Unblock the data collection.
238238
PSDataCollection<PSStreamObject> debugCollection = _debugBlockingCollection;
239-
if (debugCollection != null)
240-
{
241-
debugCollection.Complete();
242-
}
239+
debugCollection?.Complete();
243240

244241
// Unblock any new command wait.
245242
_newRunningScriptEvent.Set();
@@ -334,9 +331,8 @@ private void HostWriteLine(string line)
334331
private void AddDataEventHandlers()
335332
{
336333
// Create new collection objects.
337-
if (_debugBlockingCollection != null) { _debugBlockingCollection.Dispose(); }
338-
339-
if (_debugAccumulateCollection != null) { _debugAccumulateCollection.Dispose(); }
334+
_debugBlockingCollection?.Dispose();
335+
_debugAccumulateCollection?.Dispose();
340336

341337
_debugBlockingCollection = new PSDataCollection<PSStreamObject>();
342338
_debugBlockingCollection.BlockingEnumerator = true;

src/Microsoft.PowerShell.Commands.Utility/commands/utility/ExportAliasCommand.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ protected override void EndProcessing()
291291
line = GetAliasLine(alias, "set-alias -Name:\"{0}\" -Value:\"{1}\" -Description:\"{2}\" -Option:\"{3}\"");
292292
}
293293

294-
if (writer != null)
295-
writer.WriteLine(line);
294+
writer?.WriteLine(line);
296295

297296
if (PassThru)
298297
{
@@ -302,8 +301,7 @@ protected override void EndProcessing()
302301
}
303302
finally
304303
{
305-
if (writer != null)
306-
writer.Dispose();
304+
writer?.Dispose();
307305
// reset the read-only attribute
308306
if (readOnlyFileInfo != null)
309307
readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;

src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/OutWindowProxy.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,7 @@ internal void ShowWindow()
230230
}
231231
}
232232

233-
internal void BlockUntilClosed()
234-
{
235-
if (_closedEvent != null)
236-
{
237-
_closedEvent.WaitOne();
238-
}
239-
}
233+
internal void BlockUntilClosed() => _closedEvent?.WaitOne();
240234

241235
/// <summary>
242236
/// Implements IDisposable logic.

src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowMarkdownCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ private void ProcessMarkdownInfo(MarkdownInfo markdownInfo)
224224
/// </summary>
225225
protected override void EndProcessing()
226226
{
227-
if (_powerShell != null)
228-
{
229-
_powerShell.Dispose();
230-
}
227+
_powerShell?.Dispose();
231228
}
232229
}
233230
}

src/Microsoft.PowerShell.Commands.Utility/commands/utility/StartSleepCommand.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ private void Sleep(int milliSecondsToSleep)
9191
}
9292
}
9393

94-
if (_waitHandle != null)
95-
{
96-
_waitHandle.WaitOne(milliSecondsToSleep, true);
97-
}
94+
_waitHandle?.WaitOne(milliSecondsToSleep, true);
9895
}
9996

10097
/// <summary>
@@ -150,10 +147,7 @@ protected override void StopProcessing()
150147
lock (_syncObject)
151148
{
152149
_stopping = true;
153-
if (_waitHandle != null)
154-
{
155-
_waitHandle.Set();
156-
}
150+
_waitHandle?.Set();
157151
}
158152
}
159153

src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,10 +1160,7 @@ protected override void ProcessRecord()
11601160
indicesToRemove.Sort();
11611161
for (int i = indicesToRemove.Count - 1; i >= 0; i--)
11621162
{
1163-
if (Context.InitialSessionState != null)
1164-
{
1165-
Context.InitialSessionState.Types.RemoveItem(indicesToRemove[i]);
1166-
}
1163+
Context.InitialSessionState?.Types.RemoveItem(indicesToRemove[i]);
11671164
}
11681165

11691166
try

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,10 +1578,7 @@ protected override void ProcessRecord()
15781578
}
15791579
finally
15801580
{
1581-
if (reader != null)
1582-
{
1583-
reader.Dispose();
1584-
}
1581+
reader?.Dispose();
15851582
}
15861583

15871584
if (!string.IsNullOrEmpty(detailMsg))
@@ -1657,13 +1654,7 @@ protected override void ProcessRecord()
16571654
/// <summary>
16581655
/// Implementing ^C, after start the BeginGetResponse.
16591656
/// </summary>
1660-
protected override void StopProcessing()
1661-
{
1662-
if (_cancelToken != null)
1663-
{
1664-
_cancelToken.Cancel();
1665-
}
1666-
}
1657+
protected override void StopProcessing() => _cancelToken?.Cancel();
16671658

16681659
#endregion Overrides
16691660

src/Microsoft.PowerShell.Commands.Utility/commands/utility/XmlCommands.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,7 @@ protected override void ProcessRecord()
453453
{
454454
CreateMemoryStream();
455455

456-
if (_serializer != null)
457-
_serializer.SerializeAsStream(InputObject);
456+
_serializer?.SerializeAsStream(InputObject);
458457

459458
if (_serializer != null)
460459
{
@@ -472,8 +471,7 @@ protected override void ProcessRecord()
472471
}
473472
else
474473
{
475-
if (_serializer != null)
476-
_serializer.Serialize(InputObject);
474+
_serializer?.Serialize(InputObject);
477475
}
478476
}
479477

@@ -801,13 +799,7 @@ internal void Import()
801799
}
802800
}
803801

804-
internal void Stop()
805-
{
806-
if (_deserializer != null)
807-
{
808-
_deserializer.Stop();
809-
}
810-
}
802+
internal void Stop() => _deserializer?.Stop();
811803
}
812804

813805
#region Select-Xml

0 commit comments

Comments
 (0)