diff --git a/src/System.Management.Automation/engine/CmdletInfo.cs b/src/System.Management.Automation/engine/CmdletInfo.cs index 05b7548f9f..659ed59978 100644 --- a/src/System.Management.Automation/engine/CmdletInfo.cs +++ b/src/System.Management.Automation/engine/CmdletInfo.cs @@ -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) diff --git a/src/System.Management.Automation/engine/CmdletParameterBinderController.cs b/src/System.Management.Automation/engine/CmdletParameterBinderController.cs index 8f85361044..c3de48a43c 100644 --- a/src/System.Management.Automation/engine/CmdletParameterBinderController.cs +++ b/src/System.Management.Automation/engine/CmdletParameterBinderController.cs @@ -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(); + ObsoleteParameterWarningList ??= new List(); ObsoleteParameterWarningList.Add(warningRecord); } diff --git a/src/System.Management.Automation/engine/ComInterop/ComEventsSink.Extended.cs b/src/System.Management.Automation/engine/ComInterop/ComEventsSink.Extended.cs index f52ded959c..0d63276a3e 100644 --- a/src/System.Management.Automation/engine/ComInterop/ComEventsSink.Extended.cs +++ b/src/System.Management.Automation/engine/ComInterop/ComEventsSink.Extended.cs @@ -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) { diff --git a/src/System.Management.Automation/engine/ComInterop/ComInvokeBinder.cs b/src/System.Management.Automation/engine/ComInterop/ComInvokeBinder.cs index 6876425acf..88e4114cd5 100644 --- a/src/System.Management.Automation/engine/ComInterop/ComInvokeBinder.cs +++ b/src/System.Management.Automation/engine/ComInterop/ComInvokeBinder.cs @@ -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; diff --git a/src/System.Management.Automation/engine/ComInterop/ComTypeClassDesc.cs b/src/System.Management.Automation/engine/ComInterop/ComTypeClassDesc.cs index fdf0751d85..2f2886d655 100644 --- a/src/System.Management.Automation/engine/ComInterop/ComTypeClassDesc.cs +++ b/src/System.Management.Automation/engine/ComInterop/ComTypeClassDesc.cs @@ -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(); - } + _sourceItfs ??= new LinkedList(); _sourceItfs.AddLast(itfName); } else { - if (_itfs == null) - { - _itfs = new LinkedList(); - } - + _itfs ??= new LinkedList(); _itfs.AddLast(itfName); } } diff --git a/src/System.Management.Automation/engine/ComInterop/IDispatchComObject.cs b/src/System.Management.Automation/engine/ComInterop/IDispatchComObject.cs index c3a16639b6..5335a18c6d 100644 --- a/src/System.Management.Automation/engine/ComInterop/IDispatchComObject.cs +++ b/src/System.Management.Automation/engine/ComInterop/IDispatchComObject.cs @@ -271,10 +271,7 @@ namespace System.Management.Automation.ComInterop internal override IList> GetMembers(IEnumerable names) { - if (names == null) - { - names = GetMemberNames(true); - } + names ??= GetMemberNames(true); Type comType = RuntimeCallableWrapper.GetType(); diff --git a/src/System.Management.Automation/engine/ComInterop/InteropServices/ComEventsMethod.cs b/src/System.Management.Automation/engine/ComInterop/InteropServices/ComEventsMethod.cs index d31ca0299c..f8ff1e0985 100644 --- a/src/System.Management.Automation/engine/ComInterop/InteropServices/ComEventsMethod.cs +++ b/src/System.Management.Automation/engine/ComInterop/InteropServices/ComEventsMethod.cs @@ -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(); } diff --git a/src/System.Management.Automation/engine/ComInterop/SplatCallSite.cs b/src/System.Management.Automation/engine/ComInterop/SplatCallSite.cs index ecbff91a36..926bb460a0 100644 --- a/src/System.Management.Automation/engine/ComInterop/SplatCallSite.cs +++ b/src/System.Management.Automation/engine/ComInterop/SplatCallSite.cs @@ -30,10 +30,7 @@ namespace System.Management.Automation.ComInterop Debug.Assert(args != null); // Create a CallSite and invoke it. - if (_site == null) - { - _site = CallSite>.Create(SplatInvokeBinder.Instance); - } + _site ??= CallSite>.Create(SplatInvokeBinder.Instance); return _site.Target(_site, _callable, args); } diff --git a/src/System.Management.Automation/engine/ComInterop/TypeUtils.cs b/src/System.Management.Automation/engine/ComInterop/TypeUtils.cs index 041d7ccfd0..04217bd0bb 100644 --- a/src/System.Management.Automation/engine/ComInterop/TypeUtils.cs +++ b/src/System.Management.Automation/engine/ComInterop/TypeUtils.cs @@ -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; diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index ed387b62a6..53ca1f46f3 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -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(); - } + results ??= new List(); results.Add(new CompletionResult( keyword.Keyword, diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 8c247de13b..2ddb472f2d 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -333,10 +333,7 @@ namespace System.Management.Automation var commandList = keyValuePair.Value as List; if (commandList != null) { - if (endResults == null) - { - endResults = new List(); - } + endResults ??= new List(); // 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)) diff --git a/src/System.Management.Automation/engine/CommandDiscovery.cs b/src/System.Management.Automation/engine/CommandDiscovery.cs index 1b000d0ec3..e9de849a4c 100644 --- a/src/System.Management.Automation/engine/CommandDiscovery.cs +++ b/src/System.Management.Automation/engine/CommandDiscovery.cs @@ -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 diff --git a/src/System.Management.Automation/engine/CommandParameter.cs b/src/System.Management.Automation/engine/CommandParameter.cs index 227795cf1d..1c58bb87e2 100644 --- a/src/System.Management.Automation/engine/CommandParameter.cs +++ b/src/System.Management.Automation/engine/CommandParameter.cs @@ -124,10 +124,7 @@ namespace System.Management.Automation /// internal void SetArgumentValue(Ast ast, object value) { - if (_argument == null) - { - _argument = new Argument(); - } + _argument ??= new Argument(); _argument.value = value; _argument.ast = ast; diff --git a/src/System.Management.Automation/engine/CommandPathSearch.cs b/src/System.Management.Automation/engine/CommandPathSearch.cs index a761aa638c..d455273cac 100644 --- a/src/System.Management.Automation/engine/CommandPathSearch.cs +++ b/src/System.Management.Automation/engine/CommandPathSearch.cs @@ -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(); + result ??= new Collection(); 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(); + result ??= new Collection(); result.Add(fileName); } } diff --git a/src/System.Management.Automation/engine/CommandProcessorBase.cs b/src/System.Management.Automation/engine/CommandProcessorBase.cs index 8033e2b367..1d0128a8b3 100644 --- a/src/System.Management.Automation/engine/CommandProcessorBase.cs +++ b/src/System.Management.Automation/engine/CommandProcessorBase.cs @@ -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; diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 434c36dfec..cf798ddabb 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -928,10 +928,7 @@ namespace System.Management.Automation } } - if (module == null) - { - module = modules[0]; - } + module ??= modules[0]; } return module; diff --git a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs index 8e178c0cb1..48836d6cc9 100644 --- a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs +++ b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs @@ -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(); } - if (goodEntries == null) - { - goodEntries = new List(); - } + goodEntries ??= new List(); List badEntries; if (!this.GetListOfStringsFromData(manifestData, null, badKey, 0, out badEntries)) @@ -1424,10 +1418,7 @@ namespace Microsoft.PowerShell.Commands badEntries = new List(); } - if (badEntries == null) - { - badEntries = new List(); - } + badEntries ??= new List(); bool presentInGoodEntries = IsPs1xmlFileHelper_IsPresentInEntries(cimModuleFile, goodEntries); bool presentInBadEntries = IsPs1xmlFileHelper_IsPresentInEntries(cimModuleFile, badEntries); diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 57a98d9aff..eaa4fe4fb1 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -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; diff --git a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs index e0241f726c..6c5eae3496 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs @@ -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 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 { diff --git a/src/System.Management.Automation/engine/Modules/NewModuleManifestCommand.cs b/src/System.Management.Automation/engine/Modules/NewModuleManifestCommand.cs index e7cb2f846b..1035b36e0e 100644 --- a/src/System.Management.Automation/engine/Modules/NewModuleManifestCommand.cs +++ b/src/System.Management.Automation/engine/Modules/NewModuleManifestCommand.cs @@ -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);