private void AnalyzeBlock(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var block = (BlockSyntax)context.Node;

            RedundantEmptyLineAnalyzer.AnalyzeBlock(context, block);

            SyntaxList <StatementSyntax> statements = block.Statements;

            FormatEachStatementOnSeparateLineAnalyzer.AnalyzeStatements(context, statements);

            if (!statements.Any())
            {
                int startLine = block.OpenBraceToken.GetSpanStartLine();
                int endLine   = block.CloseBraceToken.GetSpanEndLine();

                if ((endLine - startLine) != 1 &&
                    block
                    .DescendantTrivia(block.Span)
                    .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    context.ReportDiagnostic(DiagnosticDescriptors.FormatEmptyBlock, block.GetLocation());
                }
            }
        }
Exemple #2
0
        private void AnalyzeBlock(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var block = (BlockSyntax)context.Node;

            RedundantEmptyLineAnalyzer.AnalyzeBlock(context, block);

            FormatEachStatementOnSeparateLineAnalyzer.AnalyzeStatements(context, block.Statements);

            if (block.Parent?.IsAnyKind(SyntaxKind.SimpleLambdaExpression, SyntaxKind.ParenthesizedLambdaExpression) == true)
            {
                if (block.Statements.Count == 1 &&
                    block.Statements[0].IsAnyKind(SyntaxKind.ReturnStatement, SyntaxKind.ExpressionStatement) &&
                    block.IsSingleline())
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.SimplifyLambdaExpression,
                        block.GetLocation());

                    FadeOut(context, block);
                }

                return;
            }

            if (block.Statements.Count == 0)
            {
                int startLineIndex = block.OpenBraceToken.GetSpanStartLine();
                int endLineIndex   = block.CloseBraceToken.GetSpanEndLine();

                if ((endLineIndex - startLineIndex) != 1 &&
                    block.OpenBraceToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLine()) &&
                    block.CloseBraceToken.LeadingTrivia.All(f => f.IsWhitespaceOrEndOfLine()))
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.FormatBlock,
                        block.GetLocation());
                }
            }
        }