Use null-coalescing assignment in /engine (#17720)

This commit is contained in:
Dmitry Volodin
2022-07-25 22:11:29 +05:00
committed by GitHub
parent 7af4679191
commit dc2e682f3a
21 changed files with 64 additions and 194 deletions
@@ -440,8 +440,7 @@ namespace System.Management.Automation
ValidateArgumentsAttribute validateAttr = attribute as ValidateArgumentsAttribute;
if (validateAttr != null)
{
if (validationAttributes == null)
validationAttributes = new Collection<ValidateArgumentsAttribute>();
validationAttributes ??= new Collection<ValidateArgumentsAttribute>();
validationAttributes.Add(validateAttr);
if ((attribute is ValidateNotNullAttribute) || (attribute is ValidateNotNullOrEmptyAttribute))
{
@@ -473,8 +472,7 @@ namespace System.Management.Automation
ArgumentTransformationAttribute argumentAttr = attribute as ArgumentTransformationAttribute;
if (argumentAttr != null)
{
if (argTransformationAttributes == null)
argTransformationAttributes = new Collection<ArgumentTransformationAttribute>();
argTransformationAttributes ??= new Collection<ArgumentTransformationAttribute>();
argTransformationAttributes.Add(argumentAttr);
return;
}
@@ -2258,10 +2258,7 @@ namespace System.Management.Automation
if (!_useReflection)
{
if (_methodInvoker == null)
{
_methodInvoker = GetMethodInvoker(methodInfo);
}
_methodInvoker ??= GetMethodInvoker(methodInfo);
if (_methodInvoker != null)
{
@@ -3047,10 +3044,7 @@ namespace System.Management.Automation
{
get
{
if (_isHidden == null)
{
_isHidden = member.GetCustomAttributes(typeof(HiddenAttribute), inherit: false).Length != 0;
}
_isHidden ??= member.GetCustomAttributes(typeof(HiddenAttribute), inherit: false).Length != 0;
return _isHidden.Value;
}
@@ -1026,10 +1026,7 @@ namespace System.Management.Automation
throw PSTraceSource.NewArgumentNullException(nameof(exception));
}
if (errorId == null)
{
errorId = string.Empty;
}
errorId ??= string.Empty;
// targetObject may be null
_error = exception;
@@ -114,10 +114,7 @@ namespace System.Management.Automation
// Helper for generated code to handle running w/ no execution context
internal static bool IsStrictVersion(ExecutionContext context, int majorVersion)
{
if (context == null)
{
context = LocalPipeline.GetExecutionContextFromTLS();
}
context ??= LocalPipeline.GetExecutionContextFromTLS();
return (context != null) && context.IsStrictVersion(majorVersion);
}
@@ -219,10 +216,7 @@ namespace System.Management.Automation
{
get
{
if (_providerNames == null)
{
_providerNames = new SingleShellProviderNames();
}
_providerNames ??= new SingleShellProviderNames();
return _providerNames;
}
@@ -1660,12 +1654,9 @@ namespace System.Management.Automation
// Initialize the fixed toplevel session state and the current session state
TopLevelSessionState = EngineSessionState = new SessionStateInternal(this);
if (AuthorizationManager == null)
{
// if authorizationmanager==null, this means the configuration
// explicitly asked for dummy authorization manager.
AuthorizationManager = new AuthorizationManager(null);
}
// if authorizationmanager==null, this means the configuration
// explicitly asked for dummy authorization manager.
AuthorizationManager ??= new AuthorizationManager(null);
// Set up the module intrinsics
Modules = new ModuleIntrinsics(this);
@@ -647,14 +647,11 @@ namespace System.Management.Automation
{
get
{
if (_errorRecord == null)
{
_errorRecord = new ErrorRecord(
new ParentContainsErrorRecordException(this),
_errorId,
ErrorCategory.InvalidArgument,
null);
}
_errorRecord ??= new ErrorRecord(
new ParentContainsErrorRecordException(this),
_errorId,
ErrorCategory.InvalidArgument,
null);
return _errorRecord;
}
@@ -80,10 +80,7 @@ namespace Microsoft.PowerShell.Commands
set
{
if (value == null)
{
value = Array.Empty<string>();
}
value ??= Array.Empty<string>();
_verbs = value;
_verbPatterns = null;
@@ -106,10 +103,7 @@ namespace Microsoft.PowerShell.Commands
set
{
if (value == null)
{
value = Array.Empty<string>();
}
value ??= Array.Empty<string>();
_nouns = value;
_nounPatterns = null;
@@ -132,10 +126,7 @@ namespace Microsoft.PowerShell.Commands
set
{
if (value == null)
{
value = Array.Empty<string>();
}
value ??= Array.Empty<string>();
_modules = value;
_modulePatterns = null;
@@ -400,10 +391,7 @@ namespace Microsoft.PowerShell.Commands
}
// Initialize the module patterns
if (_modulePatterns == null)
{
_modulePatterns = SessionStateUtilities.CreateWildcardsFromStrings(Module, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
}
_modulePatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(Module, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
switch (ParameterSetName)
{
@@ -686,15 +674,9 @@ namespace Microsoft.PowerShell.Commands
do // false loop
{
if (_verbPatterns == null)
{
_verbPatterns = SessionStateUtilities.CreateWildcardsFromStrings(Verb, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
}
_verbPatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(Verb, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
if (_nounPatterns == null)
{
_nounPatterns = SessionStateUtilities.CreateWildcardsFromStrings(Noun, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
}
_nounPatterns ??= SessionStateUtilities.CreateWildcardsFromStrings(Noun, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
if (!string.IsNullOrEmpty(command.ModuleName))
{
@@ -1154,10 +1136,7 @@ namespace Microsoft.PowerShell.Commands
return true;
}
if (_matchedParameterNames == null)
{
_matchedParameterNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
_matchedParameterNames ??= new HashSet<string>(StringComparer.OrdinalIgnoreCase);
IEnumerable<ParameterMetadata> commandParameters = null;
try
@@ -96,15 +96,13 @@ namespace System.Management.Automation
{
get
{
if (this._user == null)
{
// domain\user on Windows, just user on Unix
// domain\user on Windows, just user on Unix
this._user ??=
#if UNIX
this._user = Environment.UserName;
Environment.UserName;
#else
this._user = Environment.UserDomainName + "\\" + Environment.UserName;
Environment.UserDomainName + "\\" + Environment.UserName;
#endif
}
return _user;
}
@@ -634,10 +634,7 @@ namespace System.Management.Automation
// If second can be converted to the type of the first, it does so and returns first.Equals(secondConverted)
// Otherwise false is returned
if (formatProvider == null)
{
formatProvider = CultureInfo.InvariantCulture;
}
formatProvider ??= CultureInfo.InvariantCulture;
if (!(formatProvider is CultureInfo culture))
{
@@ -781,10 +778,7 @@ namespace System.Management.Automation
/// </exception>
public static int Compare(object first, object second, bool ignoreCase, IFormatProvider formatProvider)
{
if (formatProvider == null)
{
formatProvider = CultureInfo.InvariantCulture;
}
formatProvider ??= CultureInfo.InvariantCulture;
if (!(formatProvider is CultureInfo culture))
{
@@ -901,10 +895,7 @@ namespace System.Management.Automation
public static bool TryCompare(object first, object second, bool ignoreCase, IFormatProvider formatProvider, out int result)
{
result = 0;
if (formatProvider == null)
{
formatProvider = CultureInfo.InvariantCulture;
}
formatProvider ??= CultureInfo.InvariantCulture;
if (formatProvider is not CultureInfo culture)
{
@@ -4675,7 +4666,7 @@ namespace System.Management.Automation
Type propType;
if (TypeResolver.TryResolveType(property.TypeNameOfValue, out propType))
{
if (formatProvider == null) { formatProvider = CultureInfo.InvariantCulture; }
formatProvider ??= CultureInfo.InvariantCulture;
try
{
@@ -5743,10 +5734,7 @@ namespace System.Management.Automation
}
}
if (converter == null)
{
converter = FigurePropertyConversion(fromType, toType, ref rank);
}
converter ??= FigurePropertyConversion(fromType, toType, ref rank);
if (TypeConverterPossiblyExists(fromType) || TypeConverterPossiblyExists(toType)
|| (converter != null && valueDependentConversion != null))
@@ -945,8 +945,7 @@ namespace System.Management.Automation
if (!OutVariable.StartsWith('+') &&
string.Equals("Out-Default", _commandInfo.Name, StringComparison.OrdinalIgnoreCase))
{
if (_state == null)
_state = new SessionState(Context.EngineSessionState);
_state ??= new SessionState(Context.EngineSessionState);
IList oldValue = null;
oldValue = PSObject.Base(_state.PSVariable.GetValue(this.OutVariable)) as IList;
@@ -978,8 +977,7 @@ namespace System.Management.Automation
EnsureVariableParameterAllowed();
if (_state == null)
_state = new SessionState(Context.EngineSessionState);
_state ??= new SessionState(Context.EngineSessionState);
// Create the pipeline variable
_pipelineVarReference = new PSVariable(PipelineVariable);
@@ -2562,8 +2560,7 @@ namespace System.Management.Automation
EnsureVariableParameterAllowed();
if (_state == null)
_state = new SessionState(Context.EngineSessionState);
_state ??= new SessionState(Context.EngineSessionState);
if (variableName.StartsWith('+'))
{
@@ -2276,10 +2276,7 @@ namespace System.Management.Automation
newArguments[i + 1] = arguments[i];
}
if (_codeReferenceMethodInformation == null)
{
_codeReferenceMethodInformation = DotNetAdapter.GetMethodInformationArray(new[] { CodeReference });
}
_codeReferenceMethodInformation ??= DotNetAdapter.GetMethodInformationArray(new[] { CodeReference });
Adapter.GetBestMethodAndArguments(CodeReference.Name, _codeReferenceMethodInformation, newArguments, out object[] convertedArguments);
@@ -2632,10 +2629,7 @@ namespace System.Management.Automation
return new PSMethod(name, dotNetInstanceAdapter, baseObject, method, isSpecial, isHidden);
}
if (method.PSMethodCtor == null)
{
method.PSMethodCtor = CreatePSMethodConstructor(method.methodInformationStructures);
}
method.PSMethodCtor ??= CreatePSMethodConstructor(method.methodInformationStructures);
return method.PSMethodCtor.Invoke(name, dotNetInstanceAdapter, baseObject, method, isSpecial, isHidden);
}
@@ -674,13 +674,10 @@ namespace System.Management.Automation
{
lock (_lockObject)
{
if (_instanceMembers == null)
{
_instanceMembers =
s_instanceMembersResurrectionTable.GetValue(
GetKeyForResurrectionTables(this),
_ => new PSMemberInfoInternalCollection<PSMemberInfo>());
}
_instanceMembers ??=
s_instanceMembersResurrectionTable.GetValue(
GetKeyForResurrectionTables(this),
_ => new PSMemberInfoInternalCollection<PSMemberInfo>());
}
}
@@ -721,10 +718,7 @@ namespace System.Management.Automation
{
lock (_lockObject)
{
if (_adapterSet == null)
{
_adapterSet = GetMappedAdapter(_immediateBaseObject, GetTypeTable());
}
_adapterSet ??= GetMappedAdapter(_immediateBaseObject, GetTypeTable());
}
}
@@ -743,10 +737,7 @@ namespace System.Management.Automation
{
lock (_lockObject)
{
if (_members == null)
{
_members = new PSMemberInfoIntegratingCollection<PSMemberInfo>(this, s_memberCollection);
}
_members ??= new PSMemberInfoIntegratingCollection<PSMemberInfo>(this, s_memberCollection);
}
}
@@ -765,10 +756,7 @@ namespace System.Management.Automation
{
lock (_lockObject)
{
if (_properties == null)
{
_properties = new PSMemberInfoIntegratingCollection<PSPropertyInfo>(this, s_propertyCollection);
}
_properties ??= new PSMemberInfoIntegratingCollection<PSPropertyInfo>(this, s_propertyCollection);
}
}
@@ -787,10 +775,7 @@ namespace System.Management.Automation
{
lock (_lockObject)
{
if (_methods == null)
{
_methods = new PSMemberInfoIntegratingCollection<PSMethodInfo>(this, s_methodCollection);
}
_methods ??= new PSMemberInfoIntegratingCollection<PSMethodInfo>(this, s_methodCollection);
}
}
@@ -410,10 +410,7 @@ namespace System.Management.Automation
}
}
if (propertyAttributes == null)
{
propertyAttributes = new AttributeCollection();
}
propertyAttributes ??= new AttributeCollection();
typeDescriptor.WriteLine("Adding property \"{0}\".", propertyInfo.Name);
@@ -93,14 +93,11 @@ namespace System.Management.Automation
{
get
{
if (_errorRecord == null)
{
_errorRecord = new ErrorRecord(
new ParentContainsErrorRecordException(this),
"UnauthorizedAccess",
ErrorCategory.SecurityError,
null);
}
_errorRecord ??= new ErrorRecord(
new ParentContainsErrorRecordException(this),
"UnauthorizedAccess",
ErrorCategory.SecurityError,
null);
return _errorRecord;
}
@@ -999,10 +999,7 @@ namespace System.Management.Automation
// Construct the collection type information if it wasn't passed in.
if (collectionTypeInfo == null)
{
collectionTypeInfo = new ParameterCollectionTypeInformation(toType);
}
collectionTypeInfo ??= new ParameterCollectionTypeInformation(toType);
object originalValue = currentValue;
object result = currentValue;
@@ -233,34 +233,22 @@ namespace System.Management.Automation.Internal
switch (kind)
{
case VariableStreamKind.Error:
if (_errorVariableList == null)
{
_errorVariableList = new List<IList>();
}
_errorVariableList ??= new List<IList>();
_errorVariableList.Add(list);
break;
case VariableStreamKind.Warning:
if (_warningVariableList == null)
{
_warningVariableList = new List<IList>();
}
_warningVariableList ??= new List<IList>();
_warningVariableList.Add(list);
break;
case VariableStreamKind.Output:
if (_outVariableList == null)
{
_outVariableList = new List<IList>();
}
_outVariableList ??= new List<IList>();
_outVariableList.Add(list);
break;
case VariableStreamKind.Information:
if (_informationVariableList == null)
{
_informationVariableList = new List<IList>();
}
_informationVariableList ??= new List<IList>();
_informationVariableList.Add(list);
break;
@@ -4087,10 +4087,7 @@ namespace System.Management.Automation
throw PSTraceSource.NewArgumentNullException(nameof(paths));
}
if (copyPath == null)
{
copyPath = string.Empty;
}
copyPath ??= string.Empty;
CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);
context.Force = force;
@@ -4153,10 +4150,7 @@ namespace System.Management.Automation
throw PSTraceSource.NewArgumentNullException(nameof(paths));
}
if (copyPath == null)
{
copyPath = string.Empty;
}
copyPath ??= string.Empty;
// Get the provider specific path for the destination
@@ -316,10 +316,7 @@ namespace System.Management.Automation
}
}
if (context == null)
{
context = new CmdletProviderContext(this.ExecutionContext);
}
context ??= new CmdletProviderContext(this.ExecutionContext);
if (CurrentDrive != null)
{
@@ -960,10 +960,7 @@ namespace System.Management.Automation
throw PSTraceSource.NewArgumentNullException(nameof(provider));
}
if (context == null)
{
context = new CmdletProviderContext(this.ExecutionContext);
}
context ??= new CmdletProviderContext(this.ExecutionContext);
// Initialize the provider so that it can add any drives
// that it needs.
@@ -1628,10 +1628,7 @@ namespace System.Management.Automation
internal void AddType(string name, Type type)
{
if (TypeTable == null)
{
TypeTable = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
}
TypeTable ??= new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
TypeTable[name] = type;
}
@@ -771,10 +771,7 @@ namespace System.Management.Automation.Runspaces
}
// Somewhat pointlessly (backcompat), we allow a missing Member node
if (members == null)
{
members = new Collection<TypeMemberData>();
}
members ??= new Collection<TypeMemberData>();
if (_context.errors.Count != errorCount)
{
@@ -841,10 +838,7 @@ namespace System.Management.Automation.Runspaces
{
if ((object)_reader.LocalName == (object)_idName)
{
if (referencedProperties == null)
{
referencedProperties = new List<string>(8);
}
referencedProperties ??= new List<string>(8);
referencedProperties.Add(ReadElementString(_idName));
}
@@ -3724,10 +3718,7 @@ namespace System.Management.Automation.Runspaces
if (hasStandardMembers)
{
if (typeMembers == null)
{
typeMembers = _extendedMembers.GetOrAdd(typeName, GetValueFactoryBasedOnInitCapacity(capacity: 1));
}
typeMembers ??= _extendedMembers.GetOrAdd(typeName, GetValueFactoryBasedOnInitCapacity(capacity: 1));
ProcessStandardMembers(errors, typeName, typeData.StandardMembers, propertySets, typeMembers, typeData.IsOverride);
}
@@ -1021,10 +1021,7 @@ namespace System.Management.Automation.Internal
ValidateCommandProcessorNotNull(firstcommandProcessor, PipelineStrings.PipelineExecuteRequiresAtLeastOneCommand);
// Set the execution scope using the current scope
if (_executionScope == null)
{
_executionScope = firstcommandProcessor.Context.EngineSessionState.CurrentScope;
}
_executionScope ??= firstcommandProcessor.Context.EngineSessionState.CurrentScope;
// add ExternalSuccessOutput to the last command
CommandProcessorBase LastCommandProcessor = _commands[_commands.Count - 1];