Esempio n. 1
0
        public static void Analyze(SyntaxNodeAnalysisContext context, LocalDeclarationStatementSyntax localDeclaration)
        {
            if (!localDeclaration.IsConst)
            {
                VariableDeclarationSyntax declaration = localDeclaration.Declaration;

                if (declaration != null)
                {
                    SeparatedSyntaxList <VariableDeclaratorSyntax> variables = declaration.Variables;

                    if (variables.Any())
                    {
                        SyntaxList <StatementSyntax> statements = localDeclaration.GetContainingList();

                        if (statements.Any())
                        {
                            int index = statements.IndexOf(localDeclaration);

                            if (index < statements.Count - 1)
                            {
                                StatementSyntax nextStatement = statements[index + 1];

                                if (nextStatement.IsKind(SyntaxKind.ExpressionStatement))
                                {
                                    var expressionStatement = (ExpressionStatementSyntax)nextStatement;

                                    ExpressionSyntax expression = expressionStatement.Expression;

                                    if (expression?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true)
                                    {
                                        var assignment = (AssignmentExpressionSyntax)expression;

                                        ExpressionSyntax left = assignment.Left;

                                        if (left.IsKind(SyntaxKind.IdentifierName))
                                        {
                                            ExpressionSyntax right = assignment.Right;

                                            if (right?.IsMissing == false)
                                            {
                                                VariableDeclaratorSyntax declarator = FindInitializedVariable(variables, (IdentifierNameSyntax)left, context.SemanticModel, context.CancellationToken);

                                                if (declarator != null)
                                                {
                                                    EqualsValueClauseSyntax initializer = declarator.Initializer;
                                                    ExpressionSyntax        value       = initializer?.Value;

                                                    if (value == null ||
                                                        IsDefaultValue(declaration.Type, value, context.SemanticModel, context.CancellationToken))
                                                    {
                                                        TextSpan span = TextSpan.FromBounds(
                                                            (value != null) ? value.SpanStart : declarator.Span.End,
                                                            right.Span.Start);

                                                        if (!localDeclaration.Parent.ContainsDirectives(span))
                                                        {
                                                            context.ReportDiagnostic(
                                                                DiagnosticDescriptors.MergeLocalDeclarationWithInitialization,
                                                                declarator.Identifier);

                                                            if (value != null)
                                                            {
                                                                context.ReportNode(FadeOutDescriptor, initializer);
                                                            }

                                                            context.ReportToken(FadeOutDescriptor, localDeclaration.SemicolonToken);
                                                            context.ReportNode(FadeOutDescriptor, left);

                                                            if (value != null)
                                                            {
                                                                context.ReportToken(FadeOutDescriptor, assignment.OperatorToken);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }