Use null propagation in the Utility module (#17787)

This commit is contained in:
Frode Flaten
2022-08-01 09:21:42 -07:00
committed by GitHub
parent 7c624902e1
commit ebfb62b5ec
11 changed files with 18 additions and 71 deletions
@@ -429,10 +429,7 @@ namespace Microsoft.PowerShell.Commands
_readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;
}
if (_helper != null)
{
_helper.Dispose();
}
_helper?.Dispose();
}
private void ReconcilePreexistingPropertyNames()
@@ -170,10 +170,7 @@ namespace System.Management.Automation
internal void Stop()
{
CustomInternalSerializer serializer = _serializer;
if (serializer != null)
{
serializer.Stop();
}
serializer?.Stop();
}
#endregion
@@ -236,10 +236,7 @@ namespace Microsoft.PowerShell.Commands
// Unblock the data collection.
PSDataCollection<PSStreamObject> debugCollection = _debugBlockingCollection;
if (debugCollection != null)
{
debugCollection.Complete();
}
debugCollection?.Complete();
// Unblock any new command wait.
_newRunningScriptEvent.Set();
@@ -334,9 +331,8 @@ namespace Microsoft.PowerShell.Commands
private void AddDataEventHandlers()
{
// Create new collection objects.
if (_debugBlockingCollection != null) { _debugBlockingCollection.Dispose(); }
if (_debugAccumulateCollection != null) { _debugAccumulateCollection.Dispose(); }
_debugBlockingCollection?.Dispose();
_debugAccumulateCollection?.Dispose();
_debugBlockingCollection = new PSDataCollection<PSStreamObject>();
_debugBlockingCollection.BlockingEnumerator = true;
@@ -291,8 +291,7 @@ namespace Microsoft.PowerShell.Commands
line = GetAliasLine(alias, "set-alias -Name:\"{0}\" -Value:\"{1}\" -Description:\"{2}\" -Option:\"{3}\"");
}
if (writer != null)
writer.WriteLine(line);
writer?.WriteLine(line);
if (PassThru)
{
@@ -302,8 +301,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (writer != null)
writer.Dispose();
writer?.Dispose();
// reset the read-only attribute
if (readOnlyFileInfo != null)
readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;
@@ -230,13 +230,7 @@ namespace Microsoft.PowerShell.Commands
}
}
internal void BlockUntilClosed()
{
if (_closedEvent != null)
{
_closedEvent.WaitOne();
}
}
internal void BlockUntilClosed() => _closedEvent?.WaitOne();
/// <summary>
/// Implements IDisposable logic.
@@ -224,10 +224,7 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
protected override void EndProcessing()
{
if (_powerShell != null)
{
_powerShell.Dispose();
}
_powerShell?.Dispose();
}
}
}
@@ -91,10 +91,7 @@ namespace Microsoft.PowerShell.Commands
}
}
if (_waitHandle != null)
{
_waitHandle.WaitOne(milliSecondsToSleep, true);
}
_waitHandle?.WaitOne(milliSecondsToSleep, true);
}
/// <summary>
@@ -150,10 +147,7 @@ namespace Microsoft.PowerShell.Commands
lock (_syncObject)
{
_stopping = true;
if (_waitHandle != null)
{
_waitHandle.Set();
}
_waitHandle?.Set();
}
}
@@ -1160,10 +1160,7 @@ namespace Microsoft.PowerShell.Commands
indicesToRemove.Sort();
for (int i = indicesToRemove.Count - 1; i >= 0; i--)
{
if (Context.InitialSessionState != null)
{
Context.InitialSessionState.Types.RemoveItem(indicesToRemove[i]);
}
Context.InitialSessionState?.Types.RemoveItem(indicesToRemove[i]);
}
try
@@ -1578,10 +1578,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (reader != null)
{
reader.Dispose();
}
reader?.Dispose();
}
if (!string.IsNullOrEmpty(detailMsg))
@@ -1657,13 +1654,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Implementing ^C, after start the BeginGetResponse.
/// </summary>
protected override void StopProcessing()
{
if (_cancelToken != null)
{
_cancelToken.Cancel();
}
}
protected override void StopProcessing() => _cancelToken?.Cancel();
#endregion Overrides
@@ -453,8 +453,7 @@ namespace Microsoft.PowerShell.Commands
{
CreateMemoryStream();
if (_serializer != null)
_serializer.SerializeAsStream(InputObject);
_serializer?.SerializeAsStream(InputObject);
if (_serializer != null)
{
@@ -472,8 +471,7 @@ namespace Microsoft.PowerShell.Commands
}
else
{
if (_serializer != null)
_serializer.Serialize(InputObject);
_serializer?.Serialize(InputObject);
}
}
@@ -801,13 +799,7 @@ namespace Microsoft.PowerShell.Commands
}
}
internal void Stop()
{
if (_deserializer != null)
{
_deserializer.Stop();
}
}
internal void Stop() => _deserializer?.Stop();
}
#region Select-Xml
@@ -249,13 +249,7 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Ensures that the sub-pipeline we created gets stopped as well.
/// </summary>
protected override void StopProcessing()
{
if (_pipeline != null)
{
_pipeline.Stop();
}
}
protected override void StopProcessing() => _pipeline?.Stop();
#endregion Cmdlet code