Repository: dotnet/roslyn
File: src/Analyzers/Core/CodeFixes/GenerateMember/AbstractGenerateMemberService.cs (C#)

Code before:

            // If the expression is used in an address-of operator for a function pointer,
            // the method must be static (CS8759).
            if (!isStatic && IsInFunctionPointerAddressOfContext(semanticDocument, expression, semanticModel, cancellationToken))
            {
                isStatic = true;
            }
        }
    }

    private static bool IsInFunctionPointerAddressOfContext(
        SemanticDocument semanticDocument,
        SyntaxNode expression,
        SemanticModel semanticModel,
        CancellationToken cancellationToken)
    {
        // Check if this expression is the operand of an address-of operator
        // that's being used to create a function pointer
        if (expression.Parent == null)
            return false;

        var syntaxKinds = semanticDocument.Document.GetLanguageService<ISyntaxKindsService>();
        if (syntaxKinds == null || expression.Parent.RawKind != syntaxKinds.AddressOfExpression)
            return false;

        var typeInfo = semanticModel.GetTypeInfo(expression.Parent, cancellationToken);
        return typeInfo.Type is IFunctionPointerTypeSymbol;
    }

    private static void DetermineTypeToGenerateInWorker(
        SemanticModel semanticModel,
        SyntaxNode expression,
        out INamedTypeSymbol? typeToGenerateIn,
        out bool isStatic,
        out bool isColorColorCase,
        CancellationToken cancellationToken)
    {
        var typeInfo = semanticModel.GetTypeInfo(expression, cancellationToken);
        var semanticInfo = semanticModel.GetSymbolInfo(expression, cancellationToken);

        typeToGenerateIn = typeInfo.Type is ITypeParameterSymbol typeParameter
            ? typeParameter.GetNamedTypeSymbolConstraint()
            : typeInfo.Type as INamedTypeSymbol;

        isStatic = semanticInfo.Symbol is INamedTypeSymbol;
        isColorColorCase = typeInfo.Type != null && semanticInfo.Symbol != null && semanticInfo.Symbol.Name == typeInfo.Type.Name;
    }
}


Review comment (suggestion):
@copilot no need to do this type check.  If we're ever generating a method for an adress-of operation, it must always be static no matter what

Code after:
            if (syntaxFacts.IsInStaticContext(expression))
                return true;

            // If the expression is used in an '&' operator, the method must be static.
            if (expression.Parent?.RawKind == syntaxFacts.SyntaxKinds.AddressOfExpression &&
                expression.Language == LanguageNames.CSharp)
            {
                return true;
            }

            return false;
        }
    }

    private static void DetermineTypeToGenerateInWorker(
        SemanticModel semanticModel,
        SyntaxNode expression,
        out INamedTypeSymbol? typeToGenerateIn,
        out bool isStatic,
        out bool isColorColorCase,
        CancellationToken cancellationToken)
    {
        var typeInfo = semanticModel.GetTypeInfo(expression, cancellationToken);
        var semanticInfo = semanticModel.GetSymbolInfo(expression, cancellationToken);

        typeToGenerateIn = typeInfo.Type is ITypeParameterSymbol typeParameter
            ? typeParameter.GetNamedTypeSymbolConstraint()
            : typeInfo.Type as INamedTypeSymbol;

        isStatic = semanticInfo.Symbol is INamedTypeSymbol;
        isColorColorCase = typeInfo.Type != null && semanticInfo.Symbol != null && semanticInfo.Symbol.Name == typeInfo.Type.Name;
    }
}