From d204d29035abcca15393c2dca11c102faa3c3732 Mon Sep 17 00:00:00 2001
From: Frode Flaten <3436158+fflaten@users.noreply.github.com>
Date: Mon, 1 Aug 2022 18:20:09 +0200
Subject: [PATCH] Use null propagation operator in
`System.Management.Automation` (#17792)
---
.../DscSupport/CimDSCParser.cs | 10 +--
.../FormatAndOutput/common/ComplexWriter.cs | 5 +-
.../common/FormattingObjectsDeserializer.cs | 4 +-
.../FormatAndOutput/common/OutputManager.cs | 9 +--
.../cmdletization/ObjectModelWrapper.cs | 5 +-
.../help/CabinetNativeApi.cs | 5 +-
.../help/SaveHelpCommand.cs | 10 +--
.../help/UpdateHelpCommand.cs | 5 +-
.../namespaces/AliasProvider.cs | 6 +-
.../namespaces/CoreCommandContext.cs | 69 ++++---------------
.../namespaces/FileSystemContentStream.cs | 27 +++-----
.../namespaces/FileSystemProvider.cs | 10 +--
.../utils/CryptoUtils.cs | 17 +----
.../utils/ObjectStream.cs | 12 +---
.../utils/RuntimeException.cs | 3 +-
15 files changed, 42 insertions(+), 155 deletions(-)
diff --git a/src/System.Management.Automation/DscSupport/CimDSCParser.cs b/src/System.Management.Automation/DscSupport/CimDSCParser.cs
index 3d1b0d0d01..ccefa7e24b 100644
--- a/src/System.Management.Automation/DscSupport/CimDSCParser.cs
+++ b/src/System.Management.Automation/DscSupport/CimDSCParser.cs
@@ -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);
}
}
diff --git a/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs
index 0982a8e198..0ffc713cb9 100644
--- a/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs
+++ b/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs
@@ -239,10 +239,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
public void Dispose()
{
- if (_mgr != null)
- {
- _mgr.RemoveStackFrame();
- }
+ _mgr?.RemoveStackFrame();
}
private readonly IndentationManager _mgr;
diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs
index 962cf6f5ff..f26a20ae6a 100644
--- a/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs
+++ b/src/System.Management.Automation/FormatAndOutput/common/FormattingObjectsDeserializer.cs
@@ -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;
}
diff --git a/src/System.Management.Automation/FormatAndOutput/common/OutputManager.cs b/src/System.Management.Automation/FormatAndOutput/common/OutputManager.cs
index 41ea611f19..a551ef89be 100644
--- a/src/System.Management.Automation/FormatAndOutput/common/OutputManager.cs
+++ b/src/System.Management.Automation/FormatAndOutput/common/OutputManager.cs
@@ -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;
}
}
diff --git a/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs b/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs
index a840ca527f..ab6b741280 100644
--- a/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs
+++ b/src/System.Management.Automation/cimSupport/cmdletization/ObjectModelWrapper.cs
@@ -50,10 +50,7 @@ namespace Microsoft.PowerShell.Cmdletization
delegate
{
var disposable = this as IDisposable;
- if (disposable != null)
- {
- disposable.Dispose();
- }
+ disposable?.Dispose();
};
}
}
diff --git a/src/System.Management.Automation/help/CabinetNativeApi.cs b/src/System.Management.Automation/help/CabinetNativeApi.cs
index 76e8fe853a..9abe5a894b 100644
--- a/src/System.Management.Automation/help/CabinetNativeApi.cs
+++ b/src/System.Management.Automation/help/CabinetNativeApi.cs
@@ -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();
diff --git a/src/System.Management.Automation/help/SaveHelpCommand.cs b/src/System.Management.Automation/help/SaveHelpCommand.cs
index 94803728c9..7f42a25a99 100644
--- a/src/System.Management.Automation/help/SaveHelpCommand.cs
+++ b/src/System.Management.Automation/help/SaveHelpCommand.cs
@@ -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();
}
}
}
diff --git a/src/System.Management.Automation/help/UpdateHelpCommand.cs b/src/System.Management.Automation/help/UpdateHelpCommand.cs
index 01b17cbad7..371a732149 100644
--- a/src/System.Management.Automation/help/UpdateHelpCommand.cs
+++ b/src/System.Management.Automation/help/UpdateHelpCommand.cs
@@ -322,10 +322,7 @@ namespace Microsoft.PowerShell.Commands
}
finally
{
- if (helpInfoDrive != null)
- {
- helpInfoDrive.Dispose();
- }
+ helpInfoDrive?.Dispose();
}
}
else
diff --git a/src/System.Management.Automation/namespaces/AliasProvider.cs b/src/System.Management.Automation/namespaces/AliasProvider.cs
index 8782a0d9bb..c51d8e3512 100644
--- a/src/System.Management.Automation/namespaces/AliasProvider.cs
+++ b/src/System.Management.Automation/namespaces/AliasProvider.cs
@@ -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
{
diff --git a/src/System.Management.Automation/namespaces/CoreCommandContext.cs b/src/System.Management.Automation/namespaces/CoreCommandContext.cs
index 5446b6b71c..5c62ed6fa9 100644
--- a/src/System.Management.Automation/namespaces/CoreCommandContext.cs
+++ b/src/System.Management.Automation/namespaces/CoreCommandContext.cs
@@ -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
///
/// The string that needs to be written.
///
- internal void WriteVerbose(string text)
- {
- if (_command != null)
- {
- _command.WriteVerbose(text);
- }
- }
+ internal void WriteVerbose(string text) => _command?.WriteVerbose(text);
///
/// Writes the object to the Warning pipe.
@@ -788,21 +777,9 @@ namespace System.Management.Automation
///
/// The string that needs to be written.
///
- 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);
///
/// Writes a debug string.
@@ -810,29 +787,11 @@ namespace System.Management.Automation
///
/// The String that needs to be written.
///
- 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
diff --git a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs
index 8330931954..352298107d 100644
--- a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs
+++ b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs
@@ -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();
}
///
@@ -1003,15 +1002,13 @@ namespace Microsoft.PowerShell.Commands
///
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();
}
///
@@ -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();
}
}
}
diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
index 3d28b5bb7c..3c0aeaddc7 100644
--- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs
+++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
@@ -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;
diff --git a/src/System.Management.Automation/utils/CryptoUtils.cs b/src/System.Management.Automation/utils/CryptoUtils.cs
index 5aa321fcb7..26d1fd7d17 100644
--- a/src/System.Management.Automation/utils/CryptoUtils.cs
+++ b/src/System.Management.Automation/utils/CryptoUtils.cs
@@ -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();
diff --git a/src/System.Management.Automation/utils/ObjectStream.cs b/src/System.Management.Automation/utils/ObjectStream.cs
index c9aef1b928..8bf4adc03b 100644
--- a/src/System.Management.Automation/utils/ObjectStream.cs
+++ b/src/System.Management.Automation/utils/ObjectStream.cs
@@ -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)
{
diff --git a/src/System.Management.Automation/utils/RuntimeException.cs b/src/System.Management.Automation/utils/RuntimeException.cs
index ec0efd4f06..d3e204f450 100644
--- a/src/System.Management.Automation/utils/RuntimeException.cs
+++ b/src/System.Management.Automation/utils/RuntimeException.cs
@@ -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