Use compound assignment in /System.Management.Automation (#17724)

This commit is contained in:
Dmitry Volodin
2022-07-25 22:07:33 +05:00
committed by GitHub
parent 223c6b8905
commit 8f6b75826e
18 changed files with 40 additions and 131 deletions
@@ -561,10 +561,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
get
{
if (t_classCache == null)
{
t_classCache = new Dictionary<string, DscClassCacheEntry>(StringComparer.OrdinalIgnoreCase);
}
t_classCache ??= new Dictionary<string, DscClassCacheEntry>(StringComparer.OrdinalIgnoreCase);
return t_classCache;
}
@@ -580,10 +577,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
get
{
if (t_byClassModuleCache == null)
{
t_byClassModuleCache = new Dictionary<string, Tuple<string, Version>>(StringComparer.OrdinalIgnoreCase);
}
t_byClassModuleCache ??= new Dictionary<string, Tuple<string, Version>>(StringComparer.OrdinalIgnoreCase);
return t_byClassModuleCache;
}
@@ -599,10 +593,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
get
{
if (t_byFileClassCache == null)
{
t_byFileClassCache = new Dictionary<string, List<Microsoft.Management.Infrastructure.CimClass>>(StringComparer.OrdinalIgnoreCase);
}
t_byFileClassCache ??= new Dictionary<string, List<Microsoft.Management.Infrastructure.CimClass>>(StringComparer.OrdinalIgnoreCase);
return t_byFileClassCache;
}
@@ -618,10 +609,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
get
{
if (t_scriptKeywordFileCache == null)
{
t_scriptKeywordFileCache = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
t_scriptKeywordFileCache ??= new HashSet<string>(StringComparer.OrdinalIgnoreCase);
return t_scriptKeywordFileCache;
}
@@ -1893,10 +1881,7 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal
{
if (keywordAst.Keyword.Keyword.Equals("Node"))
{
if (errorList == null)
{
errorList = new List<ParseError>();
}
errorList ??= new List<ParseError>();
errorList.Add(new ParseError(kwAst.Extent,
"ImportDscResourceInsideNode",
@@ -128,10 +128,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
}
// instantiate the cache if not done yet
if (_cache == null)
{
_cache = new FormattedObjectsCache(this.LineOutput.RequiresBuffering);
}
_cache ??= new FormattedObjectsCache(this.LineOutput.RequiresBuffering);
// no need for formatting, just process the object
FormatStartData formatStart = o as FormatStartData;
@@ -887,7 +887,7 @@ namespace System.Management.Automation
{
if (tr.conditionToken != null)
{
if (result.SelectionCondition == null) result.SelectionCondition = new List<DisplayEntry>();
result.SelectionCondition ??= new List<DisplayEntry>();
result.SelectionCondition.Add(new DisplayEntry(tr.conditionToken));
continue;
@@ -896,7 +896,7 @@ namespace System.Management.Automation
if (tr is TypeGroupReference)
continue;
if (result.TypeNames == null) result.TypeNames = new List<string>();
result.TypeNames ??= new List<string>();
result.TypeNames.Add(tr.name);
}
@@ -208,8 +208,7 @@ namespace System.Management.Automation
{
get
{
if (EntrySelectedBy == null)
EntrySelectedBy = new EntrySelectedBy { TypeNames = new List<string>() };
EntrySelectedBy ??= new EntrySelectedBy { TypeNames = new List<string>() };
return EntrySelectedBy.TypeNames;
}
}
@@ -187,8 +187,7 @@ namespace System.Management.Automation
{
get
{
if (EntrySelectedBy == null)
EntrySelectedBy = new EntrySelectedBy { TypeNames = new List<string>() };
EntrySelectedBy ??= new EntrySelectedBy { TypeNames = new List<string>() };
return EntrySelectedBy.TypeNames;
}
}
@@ -283,10 +283,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
{
// Since it is a leaf node we just consider it an empty string and go
// on with formatting
if (val == null)
{
val = string.Empty;
}
val ??= string.Empty;
FieldFormattingDirective fieldFormattingDirective = null;
StringFormatError formatErrorObject = null;
@@ -233,10 +233,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
ci.propertyName = (string)key;
}
if (ci.propertyName == null)
{
ci.propertyName = this.activeAssociationList[k].ResolvedExpression.ToString();
}
ci.propertyName ??= this.activeAssociationList[k].ResolvedExpression.ToString();
// set the width of the table
if (a.OriginatingParameter != null)
@@ -391,10 +388,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
}
}
if (matchingRowDefinition == null)
{
matchingRowDefinition = match.BestMatch as TableRowDefinition;
}
matchingRowDefinition ??= match.BestMatch as TableRowDefinition;
if (matchingRowDefinition == null)
{
@@ -412,10 +406,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
}
}
if (matchingRowDefinition == null)
{
matchingRowDefinition = match.BestMatch as TableRowDefinition;
}
matchingRowDefinition ??= match.BestMatch as TableRowDefinition;
}
}
@@ -177,8 +177,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
/// <param name="lo">LineOutput interface to write to.</param>
private void WriteProperty(int k, string propertyValue, LineOutput lo)
{
if (propertyValue == null)
propertyValue = string.Empty;
propertyValue ??= string.Empty;
// make sure we honor embedded newlines
List<string> lines = StringManipulationHelper.SplitLines(propertyValue);
@@ -194,8 +193,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
prependString = _propertyLabels[k];
else
{
if (padding == null)
padding = StringUtil.Padding(_propertyLabelsDisplayLength);
padding ??= StringUtil.Padding(_propertyLabelsDisplayLength);
prependString = padding;
}
@@ -213,10 +211,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
/// <param name="lo">LineOutput to write to.</param>
private void WriteSingleLineHelper(string prependString, string line, LineOutput lo)
{
if (line is null)
{
line = string.Empty;
}
line ??= string.Empty;
// compute the width of the field for the value string (in screen cells)
int fieldCellCount = _columnWidth - _propertyLabelsDisplayLength;
@@ -21,10 +21,7 @@ namespace System.Management.Automation.Internal
{
get
{
if (_plaintextcontent == null)
{
_plaintextcontent = ValueStringDecorated.AnsiRegex.Replace(_text, string.Empty);
}
_plaintextcontent ??= ValueStringDecorated.AnsiRegex.Replace(_text, string.Empty);
return _plaintextcontent;
}
@@ -94,10 +91,7 @@ namespace System.Management.Automation.Internal
{
get
{
if (_plaintextcontent == null)
{
_plaintextcontent = AnsiRegex.Replace(_text, string.Empty);
}
_plaintextcontent ??= AnsiRegex.Replace(_text, string.Empty);
return _plaintextcontent;
}
@@ -326,15 +326,12 @@ namespace Microsoft.PowerShell.Commands
}
else
{
if (_getValueDynamicSite == null)
{
_getValueDynamicSite =
CallSite<Func<CallSite, object, object>>.Create(
PSGetMemberBinder.Get(
_stringValue,
classScope: (Type)null,
@static: false));
}
_getValueDynamicSite ??=
CallSite<Func<CallSite, object, object>>.Create(
PSGetMemberBinder.Get(
_stringValue,
classScope: (Type)null,
@static: false));
result = _getValueDynamicSite.Target.Invoke(_getValueDynamicSite, target);
}
@@ -356,15 +356,9 @@ namespace System.Management.Automation
string synopsis = Synopsis;
string detailedDescription = DetailedDescription;
if (synopsis == null)
{
synopsis = string.Empty;
}
synopsis ??= string.Empty;
if (detailedDescription == null)
{
detailedDescription = string.Empty;
}
detailedDescription ??= string.Empty;
return pattern.IsMatch(synopsis) || pattern.IsMatch(detailedDescription);
}
@@ -167,15 +167,9 @@ namespace System.Management.Automation
string synopsis = Synopsis;
string detailedDescription = DetailedDescription;
if (synopsis == null)
{
synopsis = string.Empty;
}
synopsis ??= string.Empty;
if (detailedDescription == null)
{
detailedDescription = string.Empty;
}
detailedDescription ??= string.Empty;
return pattern.IsMatch(synopsis) || pattern.IsMatch(detailedDescription);
}
@@ -5116,10 +5116,7 @@ namespace Microsoft.PowerShell.Commands
throw PSTraceSource.NewArgumentException(nameof(path));
}
if (basePath == null)
{
basePath = string.Empty;
}
basePath ??= string.Empty;
s_tracer.WriteLine("basePath = {0}", basePath);
@@ -5308,10 +5305,7 @@ namespace Microsoft.PowerShell.Commands
return string.Empty;
}
if (basePath == null)
{
basePath = string.Empty;
}
basePath ??= string.Empty;
s_tracer.WriteLine("basePath = {0}", basePath);
@@ -6248,10 +6242,7 @@ namespace Microsoft.PowerShell.Commands
if (member != null)
{
value = member.Value;
if (result == null)
{
result = new PSObject();
}
result ??= new PSObject();
result.Properties.Add(new PSNoteProperty(property, value));
}
@@ -515,10 +515,7 @@ namespace System.Management.Automation.Provider
return string.Empty;
}
if (basePath == null)
{
basePath = string.Empty;
}
basePath ??= string.Empty;
providerBaseTracer.WriteLine("basePath = {0}", basePath);
@@ -2996,10 +2996,7 @@ namespace Microsoft.PowerShell.Commands
// If properties were not specified, get all the values
if (propertyNames == null)
{
propertyNames = new Collection<string>();
}
propertyNames ??= new Collection<string>();
if (propertyNames.Count == 0 && getAll)
{
@@ -258,10 +258,7 @@ namespace Microsoft.PowerShell
//
using (Aes aes = Aes.Create())
{
if (iv is null)
{
iv = aes.IV;
}
iv ??= aes.IV;
//
// get clear text data from the input SecureString
@@ -77,10 +77,7 @@ namespace System.Management.Automation.Security
{
lock (s_systemLockdownPolicyLock)
{
if (s_systemLockdownPolicy == null)
{
s_systemLockdownPolicy = GetLockdownPolicy(path: null, handle: null);
}
s_systemLockdownPolicy ??= GetLockdownPolicy(path: null, handle: null);
}
}
else if (s_allowDebugOverridePolicy)
@@ -118,25 +118,13 @@ namespace System.Management.Automation
version = new Version("0.0");
}
if (types == null)
{
types = new Collection<string>();
}
types ??= new Collection<string>();
if (formats == null)
{
formats = new Collection<string>();
}
formats ??= new Collection<string>();
if (descriptionFallback == null)
{
descriptionFallback = string.Empty;
}
descriptionFallback ??= string.Empty;
if (vendorFallback == null)
{
vendorFallback = string.Empty;
}
vendorFallback ??= string.Empty;
Name = name;
IsDefault = isDefault;