Use null propagation operator in System.Management.Automation (#17792)

This commit is contained in:
Frode Flaten
2022-08-01 09:20:09 -07:00
committed by GitHub
parent 0d52375adc
commit d204d29035
15 changed files with 42 additions and 155 deletions
@@ -970,10 +970,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
// Ignore modules with invalid schemas.
s_tracer.WriteLine("DSC ClassCache: Error importing file '{0}', with error '{1}'. Skipping file.", path, e);
if (errors != null)
{
errors.Add(e);
}
errors?.Add(e);
}
if (classes != null)
@@ -1000,10 +997,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
ParserStrings.DuplicateCimClassDefinition, className, path, files);
e.SetErrorId("DuplicateCimClassDefinition");
if (errors != null)
{
errors.Add(e);
}
errors?.Add(e);
}
}
@@ -239,10 +239,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
public void Dispose()
{
if (_mgr != null)
{
_mgr.RemoveStackFrame();
}
_mgr?.RemoveStackFrame();
}
private readonly IndentationManager _mgr;
@@ -325,9 +325,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
internal FormatInfoData DeserializeObject(PSObject so)
{
FormatInfoData fid = FormatInfoDataClassFactory.CreateInstance(so, this);
if (fid != null)
fid.Deserialize(so, this);
fid?.Deserialize(so, this);
return fid;
}
@@ -92,19 +92,14 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
internal override void EndProcessing()
{
// shut down only if we ever processed a pipeline object
if (_mgr != null)
_mgr.ShutDown();
_mgr?.ShutDown();
}
internal override void StopProcessing()
{
lock (_syncRoot)
{
if (_lo != null)
{
_lo.StopProcessing();
}
_lo?.StopProcessing();
_isStopped = true;
}
}
@@ -50,10 +50,7 @@ namespace Microsoft.PowerShell.Cmdletization
delegate
{
var disposable = this as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
disposable?.Dispose();
};
}
}
@@ -70,10 +70,7 @@ namespace System.Management.Automation.Internal
}
// Free managed objects within 'if (disposing)' if needed
if (fdiContext != null)
{
fdiContext.Dispose();
}
fdiContext?.Dispose();
// Free unmanaged objects here
this.CleanUpDelegates();
@@ -260,10 +260,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (helpInfoDrive != null)
{
helpInfoDrive.Dispose();
}
helpInfoDrive?.Dispose();
}
}
@@ -407,10 +404,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (helpContentDrive != null)
{
helpContentDrive.Dispose();
}
helpContentDrive?.Dispose();
}
}
}
@@ -322,10 +322,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (helpInfoDrive != null)
{
helpInfoDrive.Dispose();
}
helpInfoDrive?.Dispose();
}
}
else
@@ -194,11 +194,7 @@ namespace Microsoft.PowerShell.Commands
if (dynamicParametersSpecified)
{
item = (AliasInfo)GetSessionStateItem(name);
if (item != null)
{
item.SetOptions(dynamicParameters.Options, Force);
}
item?.SetOptions(dynamicParameters.Options, Force);
}
else
{
@@ -390,13 +390,8 @@ namespace System.Management.Automation
Filter = context.Filter;
}
internal void RemoveStopReferral()
{
if (_copiedContext != null)
{
_copiedContext.StopReferrals.Remove(this);
}
}
internal void RemoveStopReferral() => _copiedContext?.StopReferrals.Remove(this);
#endregion Internal properties
#region Public properties
@@ -774,13 +769,7 @@ namespace System.Management.Automation
/// <param name="text">
/// The string that needs to be written.
/// </param>
internal void WriteVerbose(string text)
{
if (_command != null)
{
_command.WriteVerbose(text);
}
}
internal void WriteVerbose(string text) => _command?.WriteVerbose(text);
/// <summary>
/// Writes the object to the Warning pipe.
@@ -788,21 +777,9 @@ namespace System.Management.Automation
/// <param name="text">
/// The string that needs to be written.
/// </param>
internal void WriteWarning(string text)
{
if (_command != null)
{
_command.WriteWarning(text);
}
}
internal void WriteWarning(string text) => _command?.WriteWarning(text);
internal void WriteProgress(ProgressRecord record)
{
if (_command != null)
{
_command.WriteProgress(record);
}
}
internal void WriteProgress(ProgressRecord record) => _command?.WriteProgress(record);
/// <summary>
/// Writes a debug string.
@@ -810,29 +787,11 @@ namespace System.Management.Automation
/// <param name="text">
/// The String that needs to be written.
/// </param>
internal void WriteDebug(string text)
{
if (_command != null)
{
_command.WriteDebug(text);
}
}
internal void WriteDebug(string text) => _command?.WriteDebug(text);
internal void WriteInformation(InformationRecord record)
{
if (_command != null)
{
_command.WriteInformation(record);
}
}
internal void WriteInformation(InformationRecord record) => _command?.WriteInformation(record);
internal void WriteInformation(object messageData, string[] tags)
{
if (_command != null)
{
_command.WriteInformation(messageData, tags);
}
}
internal void WriteInformation(object messageData, string[] tags) => _command?.WriteInformation(messageData, tags);
#endregion User feedback mechanisms
@@ -1154,14 +1113,10 @@ namespace System.Management.Automation
{
Stopping = true;
if (_providerInstance != null)
{
// We don't need to catch any of the exceptions here because
// we are terminating the pipeline and any exception will
// be caught by the engine.
_providerInstance.StopProcessing();
}
// We don't need to catch any of the exceptions here because
// we are terminating the pipeline and any exception will
// be caught by the engine.
_providerInstance?.StopProcessing();
// Call the stop referrals if any
@@ -987,9 +987,8 @@ namespace Microsoft.PowerShell.Commands
// Seek to the place we last left off.
_stream.Seek(_fileOffset, SeekOrigin.Begin);
if (_reader != null) { _reader.DiscardBufferedData(); }
if (_backReader != null) { _backReader.DiscardBufferedData(); }
_reader?.DiscardBufferedData();
_backReader?.DiscardBufferedData();
}
/// <summary>
@@ -1003,15 +1002,13 @@ namespace Microsoft.PowerShell.Commands
/// </param>
public void Seek(long offset, SeekOrigin origin)
{
if (_writer != null) { _writer.Flush(); }
_writer?.Flush();
_stream.Seek(offset, origin);
if (_writer != null) { _writer.Flush(); }
if (_reader != null) { _reader.DiscardBufferedData(); }
if (_backReader != null) { _backReader.DiscardBufferedData(); }
_writer?.Flush();
_reader?.DiscardBufferedData();
_backReader?.DiscardBufferedData();
}
/// <summary>
@@ -1135,14 +1132,10 @@ namespace Microsoft.PowerShell.Commands
{
if (isDisposing)
{
if (_stream != null)
_stream.Dispose();
if (_reader != null)
_reader.Dispose();
if (_backReader != null)
_backReader.Dispose();
if (_writer != null)
_writer.Dispose();
_stream?.Dispose();
_reader?.Dispose();
_backReader?.Dispose();
_writer?.Dispose();
}
}
}
@@ -4520,10 +4520,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (wStream != null)
{
wStream.Dispose();
}
wStream?.Dispose();
// If copying the file from the remote session failed, then remove it.
if (errorWhileCopyRemoteFile && File.Exists(destinationFile.FullName))
@@ -4803,10 +4800,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
if (fStream != null)
{
fStream.Dispose();
}
fStream?.Dispose();
}
return success;
@@ -616,15 +616,8 @@ namespace System.Management.Automation.Internal
{
if (disposing)
{
if (_rsa != null)
{
_rsa.Dispose();
}
if (_aes != null)
{
_aes.Dispose();
}
_rsa?.Dispose();
_aes?.Dispose();
}
}
@@ -851,11 +844,7 @@ namespace System.Management.Automation.Internal
{
if (disposing)
{
if (_rsaCryptoProvider != null)
{
_rsaCryptoProvider.Dispose();
}
_rsaCryptoProvider?.Dispose();
_rsaCryptoProvider = null;
_keyExchangeCompleted.Dispose();
@@ -1496,16 +1496,8 @@ namespace System.Management.Automation.Internal
_writeHandle.Dispose();
_writeClosedHandle.Dispose();
_readClosedHandle.Dispose();
if (_readWaitHandle != null)
{
_readWaitHandle.Dispose();
}
if (_writeWaitHandle != null)
{
_writeWaitHandle.Dispose();
}
_readWaitHandle?.Dispose();
_writeWaitHandle?.Dispose();
if (_reader != null)
{
@@ -211,8 +211,7 @@ namespace System.Management.Automation
internal void SetTargetObject(object targetObject)
{
_targetObject = targetObject;
if (_errorRecord != null)
_errorRecord.SetTargetObject(targetObject);
_errorRecord?.SetTargetObject(targetObject);
}
#endregion ErrorRecord