Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs (C#)

Code before:
            var lastAst = completionContext.RelatedAsts.Last();

            PipelineAst conditionPipeline = null;
            Ast switchAst = null;

            // Check if we're in a switch statement (complete) or error statement (incomplete switch)
            if (lastAst.Parent is SwitchStatementAst switchStatementAst)
            {
                // Verify that the lastAst is one of the clause conditions (not in the body)
                bool isClauseCondition = switchStatementAst.Clauses.Any(clause => clause.Item1 == lastAst);

                if (!isClauseCondition)
                {
                    return null;
                }

                conditionPipeline = switchStatementAst.Condition as PipelineAst;
                switchAst = switchStatementAst;
            }
            else
            {
                // Check for incomplete switch parsed as ErrorStatementAst
                var errorStatementAst = lastAst.Parent as ErrorStatementAst;
                if (errorStatementAst == null || errorStatementAst.Kind == null ||
                    errorStatementAst.Kind.Kind != TokenKind.Switch)
                {
                    return null;
                }

                // For ErrorStatementAst, the case value is in Bodies, condition is in Conditions
                bool isInBodies = errorStatementAst.Bodies != null && errorStatementAst.Bodies.Any(body => body == lastAst);

                if (!isInBodies)
                {
                    return null;
                }

                // Get the condition from ErrorStatementAst.Conditions
                if (errorStatementAst.Conditions != null && errorStatementAst.Conditions.Count > 0)
                {
                    conditionPipeline = errorStatementAst.Conditions[0] as PipelineAst;
                }
                switchAst = errorStatementAst;
            }

            if (conditionPipeline == null || conditionPipeline.PipelineElements.Count != 1)
            {
                return null;
            }

            var commandExpressionAst = conditionPipeline.PipelineElements[0] as CommandExpressionAst;

Review comment (suggestion):
Please use new syntax:
```suggestion
                if (lastAst.Parent is not ErrorStatementAst errorStatementAst || errorStatementAst.Kind == null ||
                    errorStatementAst.Kind.Kind != TokenKind.Switch)
                {
```

Code after:
            PipelineAst conditionPipeline = null;
            Ast switchAst = null;

            // Check if we're in a switch statement (complete) or error statement (incomplete switch)
            if (lastAst.Parent is SwitchStatementAst switchStatementAst)
            {
                // Verify that the lastAst is one of the clause conditions (not in the body)
                bool isClauseCondition = switchStatementAst.Clauses.Any(clause => clause.Item1 == lastAst);

                if (!isClauseCondition)
                {
                    return null;
                }

                conditionPipeline = switchStatementAst.Condition as PipelineAst;
                switchAst = switchStatementAst;
            }
            else
            {
                // Check for incomplete switch parsed as ErrorStatementAst
                if (lastAst.Parent is not ErrorStatementAst errorStatementAst || errorStatementAst.Kind is null ||
                    errorStatementAst.Kind.Kind != TokenKind.Switch)
                {
                    return null;
                }

                // For ErrorStatementAst, the case value is in Bodies, condition is in Conditions
                bool isInBodies = errorStatementAst.Bodies != null && errorStatementAst.Bodies.Any(body => body == lastAst);

                if (!isInBodies)
                {
                    return null;
                }

                // Get the condition from ErrorStatementAst.Conditions
                if (errorStatementAst.Conditions != null && errorStatementAst.Conditions.Count > 0)
                {
                    conditionPipeline = errorStatementAst.Conditions[0] as PipelineAst;
                }
                switchAst = errorStatementAst;
            }

            if (conditionPipeline == null || conditionPipeline.PipelineElements.Count != 1)
            {
                return null;
            }

            if (conditionPipeline.PipelineElements[0] is not CommandExpressionAst commandExpressionAst)
            {
                return null;
            }