Use null-coalescing assignment in the rest of /engine (#17729)

This commit is contained in:
Dmitry Volodin
2022-07-26 12:31:14 -07:00
committed by GitHub
parent 6bf8a98aed
commit 1779175935
20 changed files with 50 additions and 163 deletions
@@ -380,11 +380,8 @@ namespace System.Management.Automation
}
}
if (provider == null)
{
// No path argument, so just use the current path to choose the provider.
provider = Context.SessionState.Path.CurrentLocation.Provider;
}
// If no path argument, just use the current path to choose the provider.
provider ??= Context.SessionState.Path.CurrentLocation.Provider;
provider.GetOutputTypes(Name, providerTypes);
if (providerTypes.Count > 0)
@@ -270,8 +270,7 @@ namespace System.Management.Automation
// If this generated an exception (but we didn't have one from the non-dynamic
// parameters, report on this one.
if (reportedBindingException == null)
reportedBindingException = currentBindingException;
reportedBindingException ??= currentBindingException;
// If the cmdlet implements a ValueFromRemainingArguments parameter (VarArgs)
// bind the unbound arguments to that parameter.
@@ -1452,8 +1451,7 @@ namespace System.Management.Automation
BoundObsoleteParameterNames.Add(parameter.Parameter.Name);
if (ObsoleteParameterWarningList == null)
ObsoleteParameterWarningList = new List<WarningRecord>();
ObsoleteParameterWarningList ??= new List<WarningRecord>();
ObsoleteParameterWarningList.Add(warningRecord);
}
@@ -18,10 +18,7 @@ namespace System.Management.Automation.InteropServices
public void AddHandler(int dispid, object func)
{
ComEventsMethod method = FindMethod(dispid);
if (method == null)
{
method = AddMethod(dispid);
}
method ??= AddMethod(dispid);
if (func is Delegate d)
{
@@ -114,10 +114,7 @@ namespace System.Management.Automation.ComInterop
{
get
{
if (_paramVariants == null)
{
_paramVariants = Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");
}
_paramVariants ??= Expression.Variable(VariantArray.GetStructType(_args.Length), "paramVariants");
return _paramVariants;
}
}
@@ -140,10 +137,7 @@ namespace System.Management.Automation.ComInterop
if (isByRef)
{
// Null just means that null was supplied.
if (marshalType == null)
{
marshalType = mo.Expression.Type;
}
marshalType ??= mo.Expression.Type;
marshalType = marshalType.MakeByRefType();
}
return marshalType;
@@ -17,10 +17,7 @@ namespace System.Management.Automation.ComInterop
public object CreateInstance()
{
if (_typeObj == null)
{
_typeObj = Type.GetTypeFromCLSID(Guid);
}
_typeObj ??= Type.GetTypeFromCLSID(Guid);
return Activator.CreateInstance(Type.GetTypeFromCLSID(Guid));
}
@@ -46,19 +43,12 @@ namespace System.Management.Automation.ComInterop
if (isSourceItf)
{
if (_sourceItfs == null)
{
_sourceItfs = new LinkedList<string>();
}
_sourceItfs ??= new LinkedList<string>();
_sourceItfs.AddLast(itfName);
}
else
{
if (_itfs == null)
{
_itfs = new LinkedList<string>();
}
_itfs ??= new LinkedList<string>();
_itfs.AddLast(itfName);
}
}
@@ -271,10 +271,7 @@ namespace System.Management.Automation.ComInterop
internal override IList<KeyValuePair<string, object>> GetMembers(IEnumerable<string> names)
{
if (names == null)
{
names = GetMemberNames(true);
}
names ??= GetMemberNames(true);
Type comType = RuntimeCallableWrapper.GetType();
@@ -83,10 +83,7 @@ namespace System.Management.Automation.InteropServices
&& pi.ParameterType.HasElementType
&& pi.ParameterType.GetElementType()!.IsEnum)
{
if (targetTypes == null)
{
targetTypes = new Type?[_expectedParamsCount];
}
targetTypes ??= new Type?[_expectedParamsCount];
targetTypes[i] = pi.ParameterType.GetElementType();
}
@@ -30,10 +30,7 @@ namespace System.Management.Automation.ComInterop
Debug.Assert(args != null);
// Create a CallSite and invoke it.
if (_site == null)
{
_site = CallSite<Func<CallSite, object, object[], object>>.Create(SplatInvokeBinder.Instance);
}
_site ??= CallSite<Func<CallSite, object, object[], object>>.Create(SplatInvokeBinder.Instance);
return _site.Target(_site, _callable, args);
}
@@ -102,11 +102,9 @@ namespace System.Management.Automation.ComInterop
// try lifted conversion
if (nnExprType != convertFrom || nnConvType != convertToType)
{
method = FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly);
if (method == null)
{
method = FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
}
method =
FindConversionOperator(eMethods, nnExprType, nnConvType, implicitOnly) ??
FindConversionOperator(cMethods, nnExprType, nnConvType, implicitOnly);
if (method != null)
{
return method;
@@ -183,10 +183,7 @@ namespace System.Management.Automation
{
var astContext = ExtractAstContext(_ast, _tokens, _cursorPosition);
if (typeInferenceContext.CurrentTypeDefinitionAst == null)
{
typeInferenceContext.CurrentTypeDefinitionAst = Ast.GetAncestorTypeDefinitionAst(astContext.RelatedAsts.Last());
}
typeInferenceContext.CurrentTypeDefinitionAst ??= Ast.GetAncestorTypeDefinitionAst(astContext.RelatedAsts.Last());
ExecutionContext executionContext = typeInferenceContext.ExecutionContext;
@@ -1894,10 +1891,7 @@ namespace System.Management.Automation
usageString = Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache.GetDSCResourceUsageString(keyword);
}
if (results == null)
{
results = new List<CompletionResult>();
}
results ??= new List<CompletionResult>();
results.Add(new CompletionResult(
keyword.Keyword,
@@ -333,10 +333,7 @@ namespace System.Management.Automation
var commandList = keyValuePair.Value as List<object>;
if (commandList != null)
{
if (endResults == null)
{
endResults = new List<CompletionResult>();
}
endResults ??= new List<CompletionResult>();
// The first command might be an un-prefixed commandInfo that we get by importing a module with the -Prefix parameter,
// in that case, we should add the module name qualification because if the module is not in the module path, calling
@@ -495,8 +492,7 @@ namespace System.Management.Automation
DynamicKeywordStatementAst keywordAst = null;
for (int i = context.RelatedAsts.Count - 1; i >= 0; i--)
{
if (keywordAst == null)
keywordAst = context.RelatedAsts[i] as DynamicKeywordStatementAst;
keywordAst ??= context.RelatedAsts[i] as DynamicKeywordStatementAst;
parameterAst = (context.RelatedAsts[i] as CommandParameterAst);
if (parameterAst != null) break;
}
@@ -1624,8 +1620,7 @@ namespace System.Management.Automation
}
else
{
if (positionalParam == null)
positionalParam = param;
positionalParam ??= param;
}
}
else
@@ -5762,9 +5757,9 @@ namespace System.Management.Automation
// a MemberExpressionAst '$xml.$xml', whose parent is still a MemberExpressionAst '$xml.$xml.Save'.
// But here we DO NOT want to re-assign 'targetExpr' to be '$xml.$xml'. 'targetExpr' in this case
// should be '$xml'.
else if (targetExpr is null)
else
{
targetExpr = parentAsMemberExpression.Expression;
targetExpr ??= parentAsMemberExpression.Expression;
}
}
else if (lastAst.Parent is BinaryExpressionAst binaryExpression && context.TokenAtCursor.Kind.Equals(TokenKind.Multiply))
@@ -809,10 +809,7 @@ namespace System.Management.Automation
}
// Otherwise, invoke the CommandNotFound handler
if (result == null)
{
result = InvokeCommandNotFoundHandler(commandName, context, originalCommandName, commandOrigin);
}
result ??= InvokeCommandNotFoundHandler(commandName, context, originalCommandName, commandOrigin);
} while (false);
}
else
@@ -124,10 +124,7 @@ namespace System.Management.Automation
/// </summary>
internal void SetArgumentValue(Ast ast, object value)
{
if (_argument == null)
{
_argument = new Argument();
}
_argument ??= new Argument();
_argument.value = value;
_argument.ast = ast;
@@ -493,8 +493,7 @@ namespace System.Management.Automation
if (name.Equals(baseNames[i], StringComparison.OrdinalIgnoreCase)
|| (!Platform.IsWindows && Platform.NonWindowsIsExecutable(name)))
{
if (result == null)
result = new Collection<string>();
result ??= new Collection<string>();
result.Add(fileNames[i]);
break;
}
@@ -520,8 +519,7 @@ namespace System.Management.Automation
if (fileName.EndsWith(allowedExt, StringComparison.OrdinalIgnoreCase)
|| (!Platform.IsWindows && Platform.NonWindowsIsExecutable(fileName)))
{
if (result == null)
result = new Collection<string>();
result ??= new Collection<string>();
result.Add(fileName);
}
}
@@ -352,10 +352,7 @@ namespace System.Management.Automation
// Make sure we have a session state instance for this command.
// If one hasn't been explicitly set, then use the session state
// available on the engine execution context...
if (CommandSessionState == null)
{
CommandSessionState = Context.EngineSessionState;
}
CommandSessionState ??= Context.EngineSessionState;
// Store off the current scope
_previousScope = CommandSessionState.CurrentScope;
@@ -928,10 +928,7 @@ namespace System.Management.Automation
}
}
if (module == null)
{
module = modules[0];
}
module ??= modules[0];
}
return module;
@@ -661,11 +661,8 @@ namespace Microsoft.PowerShell.Commands
}
}
if (rootedPath == null)
{
// Check for full-qualified paths - either absolute or relative
rootedPath = ResolveRootedFilePath(name, this.Context);
}
// If null check for full-qualified paths - either absolute or relative
rootedPath ??= ResolveRootedFilePath(name, this.Context);
bool alreadyLoaded = false;
var manifestProcessingFlags = ManifestProcessingFlags.LoadElements | ManifestProcessingFlags.NullOnFirstError;
@@ -1413,10 +1410,7 @@ namespace Microsoft.PowerShell.Commands
goodEntries = new List<string>();
}
if (goodEntries == null)
{
goodEntries = new List<string>();
}
goodEntries ??= new List<string>();
List<string> badEntries;
if (!this.GetListOfStringsFromData(manifestData, null, badKey, 0, out badEntries))
@@ -1424,10 +1418,7 @@ namespace Microsoft.PowerShell.Commands
badEntries = new List<string>();
}
if (badEntries == null)
{
badEntries = new List<string>();
}
badEntries ??= new List<string>();
bool presentInGoodEntries = IsPs1xmlFileHelper_IsPresentInEntries(cimModuleFile, goodEntries);
bool presentInBadEntries = IsPs1xmlFileHelper_IsPresentInEntries(cimModuleFile, badEntries);
@@ -2564,30 +2564,11 @@ namespace Microsoft.PowerShell.Commands
// without ModuleToProcess a manifest will export everything by default
// (otherwise we want to honour exports from ModuleToProcess)
if (exportedFunctions == null)
{
exportedFunctions = MatchAll;
}
if (exportedCmdlets == null)
{
exportedCmdlets = MatchAll;
}
if (exportedVariables == null)
{
exportedVariables = MatchAll;
}
if (exportedAliases == null)
{
exportedAliases = MatchAll;
}
if (exportedDscResources == null)
{
exportedDscResources = MatchAll;
}
exportedAliases ??= MatchAll;
exportedCmdlets ??= MatchAll;
exportedDscResources ??= MatchAll;
exportedFunctions ??= MatchAll;
exportedVariables ??= MatchAll;
}
manifestInfo.Description = description;
@@ -3218,16 +3199,10 @@ namespace Microsoft.PowerShell.Commands
}
}
if (newManifestInfo.RootModule == null)
{
newManifestInfo.RootModule = manifestInfo.RootModule;
}
newManifestInfo.RootModule ??= manifestInfo.RootModule;
// If may be the case that a script has already set the PrivateData field in the module
// info object, in which case we won't overwrite it.
if (newManifestInfo.PrivateData == null)
{
newManifestInfo.PrivateData = manifestInfo.PrivateData;
}
newManifestInfo.PrivateData ??= manifestInfo.PrivateData;
// Assign the PowerShellGet related properties from the module manifest
foreach (var tag in manifestInfo.Tags)
@@ -3352,10 +3327,7 @@ namespace Microsoft.PowerShell.Commands
}
}
if (newManifestInfo.RootModuleForManifest == null)
{
newManifestInfo.RootModuleForManifest = manifestInfo.RootModuleForManifest;
}
newManifestInfo.RootModuleForManifest ??= manifestInfo.RootModuleForManifest;
if (newManifestInfo.DeclaredCmdletExports == null || newManifestInfo.DeclaredCmdletExports.Count == 0)
{
@@ -6591,10 +6563,7 @@ namespace Microsoft.PowerShell.Commands
{
string binaryPath = fileName;
modulePath = fileName;
if (binaryPath == null)
{
binaryPath = System.IO.Path.Combine(moduleBase, moduleName);
}
binaryPath ??= System.IO.Path.Combine(moduleBase, moduleName);
BinaryAnalysisResult analysisResult = GetCmdletsFromBinaryModuleImplementation(binaryPath, manifestProcessingFlags, out assemblyVersion);
detectedCmdlets = analysisResult.DetectedCmdlets;
@@ -141,10 +141,7 @@ namespace System.Management.Automation
// script scope for the ss.
// Allocate the session state instance for this module.
if (ss == null)
{
ss = new SessionState(_context, true, true);
}
ss ??= new SessionState(_context, true, true);
// Now set up the module's session state to be the current session state
SessionStateInternal oldSessionState = _context.EngineSessionState;
@@ -270,7 +267,7 @@ namespace System.Management.Automation
internal List<PSModuleInfo> GetExactMatchModules(string moduleName, bool all, bool exactMatch)
{
if (moduleName == null) { moduleName = string.Empty; }
moduleName ??= string.Empty;
return GetModuleCore(new string[] { moduleName }, all, exactMatch);
}
@@ -287,10 +284,7 @@ namespace System.Management.Automation
}
else
{
if (patterns == null)
{
patterns = new string[] { "*" };
}
patterns ??= new string[] { "*" };
foreach (string pattern in patterns)
{
@@ -1192,10 +1186,7 @@ namespace System.Management.Automation
if (string.IsNullOrEmpty(currentProcessModulePath))
{
if (currentProcessModulePath is null)
{
currentProcessModulePath = string.Empty;
}
currentProcessModulePath ??= string.Empty;
}
else
{
@@ -948,12 +948,9 @@ namespace Microsoft.PowerShell.Commands
// wildcards for exported commands that weren't specified on the command line.
if (_rootModule != null || _nestedModules != null || _requiredModules != null)
{
if (_exportedFunctions == null)
_exportedFunctions = new string[] { "*" };
if (_exportedAliases == null)
_exportedAliases = new string[] { "*" };
if (_exportedCmdlets == null)
_exportedCmdlets = new string[] { "*" };
_exportedAliases ??= new string[] { "*" };
_exportedCmdlets ??= new string[] { "*" };
_exportedFunctions ??= new string[] { "*" };
}
ValidateUriParameterValue(ProjectUri, "ProjectUri");
@@ -1030,8 +1027,7 @@ namespace Microsoft.PowerShell.Commands
result.Append(streamWriter.NewLine);
result.Append(streamWriter.NewLine);
if (_rootModule == null)
_rootModule = string.Empty;
_rootModule ??= string.Empty;
BuildModuleManifest(result, nameof(RootModule), Modules.RootModule, !string.IsNullOrEmpty(_rootModule), () => QuoteName(_rootModule), streamWriter);