private static void Analyze(
            SyntaxNodeAnalysisContext context,
            SyntaxList <StatementSyntax> statements,
            SingleLocalDeclarationStatement localDeclaration,
            ExpressionSyntax expression)
        {
            if (expression?.IsKind(SyntaxKind.IdentifierName) == true)
            {
                var identifierName = (IdentifierNameSyntax)expression;

                string name = localDeclaration.IdentifierText;

                if (string.Equals(name, identifierName.Identifier.ValueText, StringComparison.Ordinal))
                {
                    TextSpan span = TextSpan.FromBounds(expression.Span.End, statements.Last().Span.End);

                    SyntaxNode parent = localDeclaration.Statement.Parent;

                    if (!IsLocalVariableReferenced(localDeclaration.Declarator, name, parent, span, context.SemanticModel, context.CancellationToken) &&
                        !parent.ContainsDirectives(TextSpan.FromBounds(localDeclaration.Statement.SpanStart, expression.Span.End)))
                    {
                        ReportDiagnostic(context, localDeclaration, expression);
                    }
                }
            }
        }
        private static void Analyze(
            SyntaxNodeAnalysisContext context,
            SyntaxList <StatementSyntax> statements,
            SingleLocalDeclarationStatement localDeclaration,
            int index,
            ExpressionStatementSyntax expressionStatement)
        {
            ExpressionSyntax expression = expressionStatement.Expression;

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

                ExpressionSyntax left  = assignment.Left;
                ExpressionSyntax right = assignment.Right;

                if (left?.IsMissing == false &&
                    right?.IsKind(SyntaxKind.IdentifierName) == true)
                {
                    var identifierName = (IdentifierNameSyntax)right;

                    string name = localDeclaration.IdentifierText;

                    if (string.Equals(name, identifierName.Identifier.ValueText, StringComparison.Ordinal))
                    {
                        VariableDeclaratorSyntax declarator = localDeclaration.Declarator;

                        ISymbol localSymbol = context.SemanticModel.GetDeclaredSymbol(declarator, context.CancellationToken);

                        if (localSymbol?.IsErrorType() == false)
                        {
                            bool isReferenced = IsLocalVariableReferenced(localSymbol, name, left, left.Span, context.SemanticModel, context.CancellationToken);

                            if (!isReferenced &&
                                index < statements.Count - 2)
                            {
                                TextSpan span = TextSpan.FromBounds(statements[index + 2].SpanStart, statements.Last().Span.End);

                                isReferenced = IsLocalVariableReferenced(localSymbol, name, localDeclaration.Statement.Parent, span, context.SemanticModel, context.CancellationToken);
                            }

                            if (!isReferenced &&
                                !expressionStatement.SpanOrLeadingTriviaContainsDirectives())
                            {
                                ReportDiagnostic(context, localDeclaration, right);
                            }
                        }
                    }
                }
            }
        }
        public static async Task ComputeRefactoringAsync(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression)
        {
            ExpressionSyntax expression = conditionalExpression.WalkUpParentheses();

            SyntaxNode parent = expression.Parent;

            if (parent.IsKind(SyntaxKind.ReturnStatement, SyntaxKind.YieldReturnStatement))
            {
                context.RegisterRefactoring(
                    Title,
                    cancellationToken => RefactorAsync(context.Document, (StatementSyntax)parent, conditionalExpression, cancellationToken));
            }
            else
            {
                var assignment = parent as AssignmentExpressionSyntax;

                if (assignment != null)
                {
                    var expressionStatement = assignment.Parent as ExpressionStatementSyntax;

                    if (expressionStatement != null)
                    {
                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, expressionStatement, conditionalExpression, cancellationToken));
                    }
                }
                else
                {
                    SingleLocalDeclarationStatement localDeclaration;
                    if (SingleLocalDeclarationStatement.TryCreateFromValue(expression, out localDeclaration))
                    {
                        TypeSyntax type = localDeclaration.Type;

                        if (type != null)
                        {
                            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                            if (!type.IsVar ||
                                semanticModel.GetTypeSymbol(type, context.CancellationToken)?.SupportsExplicitDeclaration() == true)
                            {
                                context.RegisterRefactoring(
                                    Title,
                                    cancellationToken => RefactorAsync(context.Document, localDeclaration.Statement, conditionalExpression, semanticModel, cancellationToken));
                            }
                        }
                    }
                }
            }
        }
        private static void ReportDiagnostic(
            SyntaxNodeAnalysisContext context,
            SingleLocalDeclarationStatement localDeclaration,
            ExpressionSyntax expression)
        {
            context.ReportDiagnostic(DiagnosticDescriptors.InlineLocalVariable, localDeclaration.Statement);

            foreach (SyntaxToken modifier in localDeclaration.Modifiers)
            {
                context.ReportToken(DiagnosticDescriptors.InlineLocalVariableFadeOut, modifier);
            }

            context.ReportNode(DiagnosticDescriptors.InlineLocalVariableFadeOut, localDeclaration.Type);
            context.ReportToken(DiagnosticDescriptors.InlineLocalVariableFadeOut, localDeclaration.Identifier);
            context.ReportToken(DiagnosticDescriptors.InlineLocalVariableFadeOut, localDeclaration.EqualsToken);
            context.ReportToken(DiagnosticDescriptors.InlineLocalVariableFadeOut, localDeclaration.SemicolonToken);
            context.ReportNode(DiagnosticDescriptors.InlineLocalVariableFadeOut, expression);
        }
Exemplo n.º 5
0
        private static void Analyze(
            SyntaxNodeAnalysisContext context,
            SyntaxList <StatementSyntax> statements,
            SingleLocalDeclarationStatement localDeclaration,
            int index,
            LocalDeclarationStatementSyntax localDeclaration2)
        {
            ExpressionSyntax value2 = localDeclaration2
                                      .Declaration?
                                      .Variables
                                      .SingleOrDefault(throwException: false)?
                                      .Initializer?
                                      .Value;

            if (value2?.IsKind(SyntaxKind.IdentifierName) == true)
            {
                var identifierName = (IdentifierNameSyntax)value2;

                string name = localDeclaration.IdentifierText;

                if (string.Equals(name, identifierName.Identifier.ValueText, StringComparison.Ordinal))
                {
                    bool isReferenced = false;

                    if (index < statements.Count - 2)
                    {
                        TextSpan span = TextSpan.FromBounds(statements[index + 2].SpanStart, statements.Last().Span.End);

                        isReferenced = IsLocalVariableReferenced(localDeclaration.Declarator, name, localDeclaration.Statement.Parent, span, context.SemanticModel, context.CancellationToken);
                    }

                    if (!isReferenced &&
                        !localDeclaration2.SpanOrLeadingTriviaContainsDirectives())
                    {
                        ReportDiagnostic(context, localDeclaration, value2);
                    }
                }
            }
        }
        public static void AnalyzeLocalDeclarationStatement(SyntaxNodeAnalysisContext context)
        {
            var localDeclarationStatement = (LocalDeclarationStatementSyntax)context.Node;

            if (!localDeclarationStatement.ContainsDiagnostics &&
                !localDeclarationStatement.SpanOrTrailingTriviaContainsDirectives())
            {
                SingleLocalDeclarationStatement localDeclaration;
                if (SingleLocalDeclarationStatement.TryCreate(localDeclarationStatement, out localDeclaration))
                {
                    ExpressionSyntax value = localDeclaration.Initializer?.Value;

                    if (value != null)
                    {
                        SyntaxList <StatementSyntax> statements;
                        if (localDeclarationStatement.TryGetContainingList(out statements))
                        {
                            int index = statements.IndexOf(localDeclarationStatement);

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

                                if (!nextStatement.ContainsDiagnostics)
                                {
                                    switch (nextStatement.Kind())
                                    {
                                    case SyntaxKind.ExpressionStatement:
                                    {
                                        Analyze(context, statements, localDeclaration, index, (ExpressionStatementSyntax)nextStatement);
                                        break;
                                    }

                                    case SyntaxKind.LocalDeclarationStatement:
                                    {
                                        Analyze(context, statements, localDeclaration, index, (LocalDeclarationStatementSyntax)nextStatement);
                                        break;
                                    }

                                    case SyntaxKind.ReturnStatement:
                                    {
                                        var returnStatement = (ReturnStatementSyntax)nextStatement;
                                        if (!returnStatement.SpanOrLeadingTriviaContainsDirectives())
                                        {
                                            ExpressionSyntax expression = returnStatement.Expression;
                                            if (expression?.IsKind(SyntaxKind.IdentifierName) == true)
                                            {
                                                var identifierName = (IdentifierNameSyntax)expression;

                                                if (string.Equals(localDeclaration.IdentifierText, identifierName.Identifier.ValueText, StringComparison.Ordinal))
                                                {
                                                    ReportDiagnostic(context, localDeclaration, expression);
                                                }
                                            }
                                        }

                                        break;
                                    }

                                    case SyntaxKind.ForEachStatement:
                                    {
                                        if (value.IsSingleLine() &&
                                            !value.IsKind(SyntaxKind.ArrayInitializerExpression))
                                        {
                                            var forEachStatement = (ForEachStatementSyntax)nextStatement;
                                            Analyze(context, statements, localDeclaration, forEachStatement.Expression);
                                        }

                                        break;
                                    }

                                    case SyntaxKind.SwitchStatement:
                                    {
                                        if (value.IsSingleLine())
                                        {
                                            var switchStatement = (SwitchStatementSyntax)nextStatement;
                                            Analyze(context, statements, localDeclaration, switchStatement.Expression);
                                        }

                                        break;
                                    }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }