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());
                }
            }
        }
Exemplo n.º 2
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var switchSection = (SwitchSectionSyntax)context.Node;

            SyntaxList <StatementSyntax> statements = switchSection.Statements;

            FormatEachStatementOnSeparateLineAnalyzer.AnalyzeStatements(context, statements);

            if (switchSection.Parent?.IsKind(SyntaxKind.SwitchStatement) == true)
            {
                AnalyzeRedundantDefaultSwitchSection(context, switchSection);

                AnalyzeUnnecessaryCaseLabel(context, switchSection);
            }

            AnalyzeFirstStatement(context, switchSection);

            AnalyzeDefaultSection(context, switchSection);

            if (statements.Count > 1)
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AddBracesToSwitchSectionWithMultipleStatements,
                    Location.Create(switchSection.SyntaxTree, statements.Span));
            }
        }
Exemplo n.º 3
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());
                }
            }
        }
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var switchSection = (SwitchSectionSyntax)context.Node;

            FormatEachStatementOnSeparateLineAnalyzer.AnalyzeStatements(context, switchSection.Statements);

            if (switchSection.Parent?.IsKind(SyntaxKind.SwitchStatement) == true)
            {
                AnalyzeRedundantDefaultSwitchSection(context, switchSection);
                AnalyzeUnnecessaryCaseLabel(context, switchSection);
            }

            AnalyzeFirstStatement(context, switchSection);
        }