Beispiel #1
0
        private static bool IsRecursiveYield(YieldStatementSyntax node, SemanticModel semanticModel, IMethodSymbol method)
        {
            var methodName = method.Name;

            foreach (var ancestor in node.Ancestors())
            {
                switch (ancestor)
                {
                case MethodDeclarationSyntax _:
                {
                    return(false);
                }

                case ForEachStatementSyntax loop:
                {
                    foreach (var invocation in loop.DescendantNodes().OfType <InvocationExpressionSyntax>())
                    {
                        var calledMethod = DetectCalledMethod(semanticModel, invocation, methodName);
                        if (calledMethod is null)
                        {
                            continue;
                        }

                        if (method.IsExtensionMethod && calledMethod.IsExtensionMethod)
                        {
                            if (method.ContainingType.Equals(calledMethod.ContainingType, SymbolEqualityComparer.IncludeNullability))
                            {
                                return(true);
                            }
                        }
                        else
                        {
                            if (method.Equals(calledMethod, SymbolEqualityComparer.IncludeNullability))
                            {
                                return(true);
                            }
                        }
                    }

                    return(false);
                }
                }
            }

            return(false);
        }