Beispiel #1
0
        private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
        {
            var methodDeclaration = (MethodDeclarationSyntax)context.Node;

            BlockSyntax body = methodDeclaration.Body;

            if (body == null)
            {
                return;
            }

            ParameterListSyntax parameterList = methodDeclaration.ParameterList;

            if (parameterList == null)
            {
                return;
            }

            if (!parameterList.Parameters.Any())
            {
                return;
            }

            SyntaxList <StatementSyntax> statements = body.Statements;

            int statementCount = statements.Count;

            int index = -1;

            for (int i = 0; i < statementCount; i++)
            {
                if (IsNullCheck(statements[i]))
                {
                    index++;
                }
                else
                {
                    break;
                }
            }

            if (index == -1)
            {
                return;
            }

            if (index == statementCount - 1)
            {
                return;
            }

            TextSpan span = TextSpan.FromBounds(statements[index + 1].SpanStart, statements.Last().Span.End);

            if (body.ContainsUnbalancedIfElseDirectives(span))
            {
                return;
            }

            context.CancellationToken.ThrowIfCancellationRequested();

            ContainsYieldWalker walker = ContainsYieldWalker.GetInstance();

            walker.VisitBlock(body);

            YieldStatementSyntax yieldStatement = walker.YieldStatement;

            ContainsYieldWalker.Free(walker);

            if (yieldStatement == null)
            {
                return;
            }

            if (yieldStatement.SpanStart < statements[index].Span.End)
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context,
                                               DiagnosticDescriptors.ValidateArgumentsCorrectly,
                                               Location.Create(body.SyntaxTree, new TextSpan(statements[index + 1].SpanStart, 0)));
        }