diff --git a/src/System.Management.Automation/engine/debugger/debugger.cs b/src/System.Management.Automation/engine/debugger/debugger.cs index e8e8c84944..ced9b4ed6a 100644 --- a/src/System.Management.Automation/engine/debugger/debugger.cs +++ b/src/System.Management.Automation/engine/debugger/debugger.cs @@ -1057,12 +1057,9 @@ namespace System.Management.Automation { get { - if (_isLocalSession == null) - { - // Remote debug sessions always have a ServerRemoteHost. Otherwise it is a local session. - _isLocalSession = !(((_context.InternalHost.ExternalHost != null) && - (_context.InternalHost.ExternalHost is System.Management.Automation.Remoting.ServerRemoteHost))); - } + // Remote debug sessions always have a ServerRemoteHost. Otherwise it is a local session. + _isLocalSession ??= !((_context.InternalHost.ExternalHost != null) && + (_context.InternalHost.ExternalHost is System.Management.Automation.Remoting.ServerRemoteHost)); return _isLocalSession.Value; } @@ -1949,10 +1946,7 @@ namespace System.Management.Automation if (_preserveUnhandledDebugStopEvent) { // Lazily create the event object. - if (_preserveDebugStopEvent == null) - { - _preserveDebugStopEvent = new ManualResetEventSlim(true); - } + _preserveDebugStopEvent ??= new ManualResetEventSlim(true); // Set the event handle to non-signaled. if (!_preserveDebugStopEvent.IsSet) @@ -2152,11 +2146,8 @@ namespace System.Management.Automation { lock (_syncObject) { - if (_isSystemLockedDown == null) - { - _isSystemLockedDown = (System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy() == - System.Management.Automation.Security.SystemEnforcementMode.Enforce); - } + _isSystemLockedDown ??= (System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy() == + System.Management.Automation.Security.SystemEnforcementMode.Enforce); } } diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 19abc077b0..dab439bd5a 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -1110,10 +1110,7 @@ namespace System.Management.Automation.Language internal static Expression IsStrictMode(int version, Expression executionContext = null) { - if (executionContext == null) - { - executionContext = ExpressionCache.NullExecutionContext; - } + executionContext ??= ExpressionCache.NullExecutionContext; return Expression.Call( CachedReflectionInfo.ExecutionContext_IsStrictVersion, @@ -1758,18 +1755,15 @@ namespace System.Management.Automation.Language // Unwrap the wrapped exception var innerException = tie.InnerException; var rte = innerException as RuntimeException; - if (rte == null) - { - rte = InterpreterError.NewInterpreterExceptionWithInnerException( - null, - typeof(RuntimeException), - attributeAst.Extent, - "ExceptionConstructingAttribute", - ExtendedTypeSystem.ExceptionConstructingAttribute, - innerException, - innerException.Message, - attributeAst.TypeName.FullName); - } + rte ??= InterpreterError.NewInterpreterExceptionWithInnerException( + null, + typeof(RuntimeException), + attributeAst.Extent, + "ExceptionConstructingAttribute", + ExtendedTypeSystem.ExceptionConstructingAttribute, + innerException, + innerException.Message, + attributeAst.TypeName.FullName); InterpreterError.UpdateExceptionErrorRecordPosition(rte, attributeAst.Extent); throw rte; @@ -2054,10 +2048,7 @@ namespace System.Management.Automation.Language // The sequence points are identical optimized or not. Regardless, we want to ensure // that the list is unique no matter when the property is accessed, so make sure it is set just once. - if (scriptBlock.SequencePoints == null) - { - scriptBlock.SequencePoints = _sequencePoints.ToArray(); - } + scriptBlock.SequencePoints ??= _sequencePoints.ToArray(); } private static Action CompileTree(Expression> lambda, CompileInterpretChoice compileInterpretChoice) @@ -2126,10 +2117,7 @@ namespace System.Management.Automation.Language // Can't be exposed to untrusted input - invoking arbitrary code could result in remote code // execution. - if (lambda == null) - { - lambda = (new Compiler()).CompileSingleExpression(expressionAst, out sequencePoints, out localsTupleType); - } + lambda ??= (new Compiler()).CompileSingleExpression(expressionAst, out sequencePoints, out localsTupleType); SessionStateInternal oldSessionState = context.EngineSessionState; try @@ -3946,10 +3934,7 @@ namespace System.Management.Automation.Language // This will simply return a Linq.Expression representing the redirection. var compiledRedirection = VisitFileRedirection(fileRedirectionAst); - if (extraFileRedirectExprs == null) - { - extraFileRedirectExprs = new List(commandExpr.Redirections.Count); - } + extraFileRedirectExprs ??= new List(commandExpr.Redirections.Count); // Hold the current 'FileRedirection' instance for later use var redirectionExpr = NewTemp(typeof(FileRedirection), "fileRedirection"); @@ -6146,10 +6131,7 @@ namespace System.Management.Automation.Language } } - if (childExpr == null) - { - childExpr = Compile(convertExpressionAst.Child); - } + childExpr ??= Compile(convertExpressionAst.Child); if (typeName.FullName.Equals("PSCustomObject", StringComparison.OrdinalIgnoreCase)) { diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index 75df984ad2..4a42dd262b 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -443,8 +443,7 @@ namespace System.Management.Automation.Language private Token PeekToken() { Token token = _ungotToken ?? _tokenizer.NextToken(); - if (_ungotToken == null) - _ungotToken = token; + _ungotToken ??= token; return token; } @@ -823,10 +822,7 @@ namespace System.Management.Automation.Language SkipToken(); var statement = UsingStatementRule(token); SkipNewlinesAndSemicolons(); - if (result == null) - { - result = new List(); - } + result ??= new List(); var usingStatement = statement as UsingStatementAst; // otherwise returned statement is ErrorStatementAst. @@ -1790,10 +1786,7 @@ namespace System.Management.Automation.Language break; } - if (startExtent == null) - { - startExtent = blockNameToken.Extent; - } + startExtent ??= blockNameToken.Extent; endExtent = blockNameToken.Extent; @@ -1934,10 +1927,7 @@ namespace System.Management.Automation.Language // Track the last statement inside our loop so we don't use the EmptyPipeline // as our last statement. The last statement is used to track the extent of // this statement list. - if (firstStatement == null) - { - firstStatement = statement; - } + firstStatement ??= statement; lastStatement = statement; @@ -3231,10 +3221,7 @@ namespace System.Management.Automation.Language if (topLevel) { - if (_configurationKeywordsDefinedInThisFile == null) - { - _configurationKeywordsDefinedInThisFile = new Dictionary(); - } + _configurationKeywordsDefinedInThisFile ??= new Dictionary(); _configurationKeywordsDefinedInThisFile[keywordToAddForThisConfigurationStatement.Keyword] = keywordToAddForThisConfigurationStatement; } @@ -3589,10 +3576,7 @@ namespace System.Management.Automation.Language // ErrorRecovery: don't continue parsing the for statement. UngetToken(rParen); - if (endErrorStatement == null) - { - endErrorStatement = lParen.Extent; - } + endErrorStatement ??= lParen.Extent; ReportIncompleteInput(After(endErrorStatement), nameof(ParserStrings.MissingEndParenthesisAfterStatement), @@ -3901,10 +3885,7 @@ namespace System.Management.Automation.Language // we aren't expecting a name, we still do this so that the signature of the implementing function remains // the same. ExpressionAst originalInstanceName = instanceName; - if (instanceName == null) - { - instanceName = new StringConstantExpressionAst(nameToken.Extent, elementName, StringConstantType.BareWord); - } + instanceName ??= new StringConstantExpressionAst(nameToken.Extent, elementName, StringConstantType.BareWord); SkipNewlines(); @@ -4321,10 +4302,7 @@ namespace System.Management.Automation.Language if (astsOnError != null && astsOnError.Count > 0) { - if (nestedAsts == null) - { - nestedAsts = new List(); - } + nestedAsts ??= new List(); nestedAsts.AddRange(astsOnError); lastExtent = astsOnError.Last().Extent; @@ -4353,10 +4331,7 @@ namespace System.Management.Automation.Language var classDefn = new TypeDefinitionAst(extent, name.Value, customAttributes?.OfType(), members, TypeAttributes.Class, superClassesList); if (customAttributes != null && customAttributes.OfType().Any()) { - if (nestedAsts == null) - { - nestedAsts = new List(); - } + nestedAsts ??= new List(); // no need to report error since the error is reported in method StatementRule nestedAsts.AddRange(customAttributes.OfType()); nestedAsts.Add(classDefn); @@ -4420,10 +4395,7 @@ namespace System.Management.Automation.Language if (attribute != null) { lastAttribute = attribute; - if (startExtent == null) - { - startExtent = attribute.Extent; - } + startExtent ??= attribute.Extent; var attributeAst = attribute as AttributeAst; if (attributeAst != null) @@ -4443,10 +4415,7 @@ namespace System.Management.Automation.Language } token = PeekToken(); - if (startExtent == null) - { - startExtent = token.Extent; - } + startExtent ??= token.Extent; switch (token.Kind) { @@ -4661,10 +4630,7 @@ namespace System.Management.Automation.Language return; } - if (astsOnError == null) - { - astsOnError = new List(); - } + astsOnError ??= new List(); astsOnError.Add(errAst); } @@ -4676,10 +4642,7 @@ namespace System.Management.Automation.Language return; } - if (astsOnError == null) - { - astsOnError = new List(); - } + astsOnError ??= new List(); astsOnError.AddRange(errAsts); } @@ -5268,11 +5231,9 @@ namespace System.Management.Automation.Language SetTokenizerMode(oldTokenizerMode); } - if (baseCtorCallParams == null) - { + baseCtorCallParams ??= // Assuming implicit default ctor - baseCtorCallParams = new List(); - } + new List(); } Token lCurly = NextToken(); @@ -5561,10 +5522,7 @@ namespace System.Management.Automation.Language break; } - if (exceptionTypes == null) - { - exceptionTypes = new List(); - } + exceptionTypes ??= new List(); exceptionTypes.Add(typeConstraintAst); @@ -6067,10 +6025,7 @@ namespace System.Management.Automation.Language { SkipToken(); - if (redirections == null) - { - redirections = new RedirectionAst[CommandBaseAst.MaxRedirections]; - } + redirections ??= new RedirectionAst[CommandBaseAst.MaxRedirections]; IScriptExtent unused = null; lastRedirection = RedirectionRule(redirectionToken, redirections, ref unused); @@ -6091,10 +6046,7 @@ namespace System.Management.Automation.Language if (commandAst != null) { - if (startExtent == null) - { - startExtent = commandAst.Extent; - } + startExtent ??= commandAst.Extent; pipelineElements.Add(commandAst); } @@ -6447,10 +6399,7 @@ namespace System.Management.Automation.Language } commaToken = token; - if (commandArgs == null) - { - commandArgs = new List(); - } + commandArgs ??= new List(); commandArgs.Add(exprAst); @@ -6618,10 +6567,7 @@ namespace System.Management.Automation.Language case TokenKind.RedirectInStd: if ((context & CommandArgumentContext.CommandName) == 0) { - if (redirections == null) - { - redirections = new RedirectionAst[CommandBaseAst.MaxRedirections]; - } + redirections ??= new RedirectionAst[CommandBaseAst.MaxRedirections]; RedirectionRule((RedirectionToken)token, redirections, ref endExtent); } @@ -7223,10 +7169,7 @@ namespace System.Management.Automation.Language } } - if (expr == null) - { - expr = new TypeExpressionAst(lastAttribute.Extent, lastAttribute.TypeName); - } + expr ??= new TypeExpressionAst(lastAttribute.Extent, lastAttribute.TypeName); } for (int i = attributes.Count - 2; i >= 0; --i) diff --git a/src/System.Management.Automation/engine/parser/SafeValues.cs b/src/System.Management.Automation/engine/parser/SafeValues.cs index f76269a12d..0e41f87a31 100644 --- a/src/System.Management.Automation/engine/parser/SafeValues.cs +++ b/src/System.Management.Automation/engine/parser/SafeValues.cs @@ -548,10 +548,7 @@ namespace System.Management.Automation.Language ofs = t_context.SessionState.PSVariable.GetValue("OFS") as string; } - if (ofs == null) - { - ofs = " "; - } + ofs ??= " "; for (int offset = 0; offset < safeValues.Length; offset++) { diff --git a/src/System.Management.Automation/engine/parser/TypeResolver.cs b/src/System.Management.Automation/engine/parser/TypeResolver.cs index dd69985bd5..acbf9bef2c 100644 --- a/src/System.Management.Automation/engine/parser/TypeResolver.cs +++ b/src/System.Management.Automation/engine/parser/TypeResolver.cs @@ -370,10 +370,7 @@ namespace System.Management.Automation.Language return typeName._typeDefinitionAst.Type; } - if (context == null) - { - context = LocalPipeline.GetExecutionContextFromTLS(); - } + context ??= LocalPipeline.GetExecutionContextFromTLS(); // Use the explicitly passed-in assembly list when it's specified by the caller. // Otherwise, retrieve all currently loaded assemblies. @@ -582,10 +579,7 @@ namespace System.Management.Automation.Language internal static TypeResolutionState GetDefaultUsingState(ExecutionContext context) { - if (context == null) - { - context = LocalPipeline.GetExecutionContextFromTLS(); - } + context ??= LocalPipeline.GetExecutionContextFromTLS(); if (context != null) { diff --git a/src/System.Management.Automation/engine/parser/tokenizer.cs b/src/System.Management.Automation/engine/parser/tokenizer.cs index 43046c9d29..664dbe0ac7 100644 --- a/src/System.Management.Automation/engine/parser/tokenizer.cs +++ b/src/System.Management.Automation/engine/parser/tokenizer.cs @@ -1242,10 +1242,7 @@ namespace System.Management.Automation.Language // Don't remember these tokens, they aren't useful in $$ and $^. break; default: - if (FirstToken == null) - { - FirstToken = token; - } + FirstToken ??= token; LastToken = token; break; @@ -1815,8 +1812,7 @@ namespace System.Management.Automation.Language } else if (matchedRequires && _nestedTokensAdjustment == 0) { - if (RequiresTokens == null) - RequiresTokens = new List(); + RequiresTokens ??= new List(); RequiresTokens.Add(token); } } @@ -1953,10 +1949,7 @@ namespace System.Management.Automation.Language PSSnapinToken.StartsWith(parameter.ParameterName, StringComparison.OrdinalIgnoreCase)) { snapinSpecified = true; - if (requiredSnapins == null) - { - requiredSnapins = new List(); - } + requiredSnapins ??= new List(); break; } @@ -2222,8 +2215,7 @@ namespace System.Management.Automation.Language return; } - if (requiredModules == null) - requiredModules = new List(); + requiredModules ??= new List(); requiredModules.Add(moduleSpecification); } } @@ -2246,8 +2238,7 @@ namespace System.Management.Automation.Language } else { - if (requiredAssemblies == null) - requiredAssemblies = new List(); + requiredAssemblies ??= new List(); if (!requiredAssemblies.Contains((string)arg)) { @@ -2269,8 +2260,7 @@ namespace System.Management.Automation.Language } else { - if (requiredEditions == null) - requiredEditions = new List(); + requiredEditions ??= new List(); var edition = (string)arg; if (!Utils.IsValidPSEditionValue(edition))