Repository: dotnet/roslyn
File: src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/ParenthesizedExpressionSyntaxExtensions.cs (C#)

Code before:
        {
            return false;
        }

        // Don't change (x?.Count)... to x?.Count...
        //
        // It very much changes the semantics to have code that always executed (outside the
        // parenthesized expression) now only conditionally run depending on if 'x' is null or
        // not.
        if (expression.IsKind(SyntaxKind.ConditionalAccessExpression))
            return false;

        // Operator precedence cases:
        // - If the parent is not an expression, do not remove parentheses
        // - Otherwise, parentheses may be removed if doing so does not change operator associations.
        return parentExpression != null && !RemovalChangesAssociation(node, parentExpression, semanticModel);

        static bool ContainsConditionalAccessOnRightmostPath(ExpressionSyntax expr)
        {
            // Walk down the rightmost path of the expression tree
            for (var current = expr; current != null;)
            {
                if (current is ConditionalAccessExpressionSyntax)
                    return true;

                // For binary expressions, continue down the right side
                if (current is BinaryExpressionSyntax binaryExpr)
                {
                    current = binaryExpr.Right;
                }
                else
                {
                    // For other expressions, take the first child expression
                    current = current.ChildNodes().FirstOrDefault() as ExpressionSyntax;
                }
            }

            return false;
        }
    }

    private static bool RemovalWouldChangeConstantReferenceToTypeReference(
        ParenthesizedExpressionSyntax node, ExpressionSyntax expression,
        SemanticModel semanticModel, CancellationToken cancellationToken)
    {
        // With cases like: `if (x is (Y))` then we cannot remove the parens if it would make Y now bind to a type
        // instead of a constant.
        if (node.Parent is not ConstantPatternSyntax { Parent: IsPatternExpressionSyntax })
            return false;

        var exprSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;

Review comment (suggestion):
@copilot  change this to a conditional expression.  so:

```
current = current is BniaryExpressionSyntax binaryExpression
    ? binaryExpression.Right
    : current.ChildNodes().FirstOrDefault() as ExpressionSyntax;
```

Code after:
        {
            return false;
        }

        // Don't change (x?.Count)... to x?.Count...
        //
        // It very much changes the semantics to have code that always executed (outside the
        // parenthesized expression) now only conditionally run depending on if 'x' is null or
        // not.
        if (expression.IsKind(SyntaxKind.ConditionalAccessExpression))
            return false;

        // Operator precedence cases:
        // - If the parent is not an expression, do not remove parentheses
        // - Otherwise, parentheses may be removed if doing so does not change operator associations.
        return parentExpression != null && !RemovalChangesAssociation(node, parentExpression, semanticModel);

        static bool ContainsConditionalAccessOnRightmostPath(ExpressionSyntax expr)
        {
            // Walk down the rightmost path of the expression tree
            for (var current = expr; current != null;)
            {
                if (current is ConditionalAccessExpressionSyntax)
                    return true;

                current = current is BinaryExpressionSyntax binaryExpression
                    ? binaryExpression.Right
                    : current.ChildNodes().FirstOrDefault() as ExpressionSyntax;
            }

            return false;
        }
    }

    private static bool RemovalWouldChangeConstantReferenceToTypeReference(
        ParenthesizedExpressionSyntax node, ExpressionSyntax expression,
        SemanticModel semanticModel, CancellationToken cancellationToken)
    {
        // With cases like: `if (x is (Y))` then we cannot remove the parens if it would make Y now bind to a type
        // instead of a constant.
        if (node.Parent is not ConstantPatternSyntax { Parent: IsPatternExpressionSyntax })
            return false;

        var exprSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).Symbol;
        if (exprSymbol is not IFieldSymbol { IsConst: true })
            return false;

        // See if interpreting the same expression as a type in this location binds.
        var potentialType = semanticModel.GetSpeculativeTypeInfo(expression.SpanStart, expression, SpeculativeBindingOption.BindAsTypeOrNamespace).Type;
        return potentialType is not (null or IErrorTypeSymbol);
    }