private static ImmutableArray <IfRefactoring> Analyze(
            ExpressionStatementSyntax expressionStatement,
            IfStatementSyntax ifStatement,
            IfAnalysisOptions options)
        {
            SimpleAssignmentExpression assignment;

            if (SimpleAssignmentExpression.TryCreate(expressionStatement, out assignment))
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) == false)
                {
                    SimpleAssignmentExpression assignment1;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment1))
                    {
                        SimpleAssignmentExpression assignment2;
                        if (SimpleAssignmentExpression.TryCreate(elseClause.GetSingleStatementOrDefault(), out assignment2) &&
                            assignment1.Left.IsEquivalentTo(assignment2.Left, topLevel: false) &&
                            assignment1.Left.IsEquivalentTo(assignment.Left, topLevel: false) &&
                            options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(expressionStatement.SpanStart, ifStatement.Span.End)))
                        {
                            return(new AssignmentAndIfElseToAssignmentWithConditionalExpression(expressionStatement, assignment.Right, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray());
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
Beispiel #2
0
        protected override bool IsFixableStatement(
            StatementSyntax statement,
            string name,
            ITypeSymbol typeSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            if (statement.SpanOrLeadingTriviaContainsDirectives())
            {
                return(false);
            }

            if (!(statement is ExpressionStatementSyntax expressionStatement))
            {
                return(false);
            }

            if (!SimpleAssignmentExpression.TryCreate(expressionStatement.Expression, out SimpleAssignmentExpression simpleAssignment))
            {
                return(false);
            }

            if (name != (simpleAssignment.Left as IdentifierNameSyntax)?.Identifier.ValueText)
            {
                return(false);
            }

            if (!MemberInvocationExpression.TryCreate(simpleAssignment.Right, out MemberInvocationExpression memberInvocation))
            {
                return(false);
            }

            if (!(WalkDownMethodChain(memberInvocation).Expression is IdentifierNameSyntax identifierName))
            {
                return(false);
            }

            if (name != identifierName.Identifier.ValueText)
            {
                return(false);
            }

            if (!semanticModel.TryGetMethodInfo(memberInvocation.InvocationExpression, out MethodInfo methodInfo, cancellationToken))
            {
                return(false);
            }

            if (!methodInfo.ReturnType.Equals(typeSymbol))
            {
                return(false);
            }

            if (IsReferenced(memberInvocation.InvocationExpression, identifierName, name, semanticModel, cancellationToken))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        public static void Analyze(SyntaxNodeAnalysisContext context, LocalDeclarationStatementSyntax localDeclaration)
        {
            if (!localDeclaration.IsConst &&
                !localDeclaration.SpanOrTrailingTriviaContainsDirectives())
            {
                VariableDeclarationSyntax declaration = localDeclaration.Declaration;

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

                    if (variables.Any())
                    {
                        StatementSyntax nextStatement = localDeclaration.NextStatement();

                        if (nextStatement?.SpanOrLeadingTriviaContainsDirectives() == false)
                        {
                            SimpleAssignmentExpression assignment;
                            if (SimpleAssignmentExpression.TryCreate(nextStatement, out assignment) &&
                                assignment.Left.IsKind(SyntaxKind.IdentifierName))
                            {
                                SemanticModel     semanticModel     = context.SemanticModel;
                                CancellationToken cancellationToken = context.CancellationToken;

                                VariableDeclaratorSyntax declarator = FindInitializedVariable((IdentifierNameSyntax)assignment.Left, variables, semanticModel, cancellationToken);

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

                                    if (value == null ||
                                        IsDefaultValue(declaration.Type, value, semanticModel, cancellationToken))
                                    {
                                        context.ReportDiagnostic(DiagnosticDescriptors.MergeLocalDeclarationWithAssignment, declarator.Identifier);

                                        if (value != null)
                                        {
                                            context.ReportNode(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, initializer);
                                            context.ReportToken(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, assignment.Expression.OperatorToken);
                                        }

                                        context.ReportToken(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, localDeclaration.SemicolonToken);
                                        context.ReportNode(DiagnosticDescriptors.MergeLocalDeclarationWithAssignmentFadeOut, assignment.Left);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        public static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
        {
            var ifStatement = (IfStatementSyntax)context.Node;

            if (ifStatement.IsSimpleIf() &&
                !IsPartOfLazyInitialization(ifStatement))
            {
                EqualsToNullExpression equalsToNull;
                if (EqualsToNullExpression.TryCreate(ifStatement.Condition, out equalsToNull))
                {
                    SimpleAssignmentExpression assignment;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment) &&
                        assignment.Left.IsEquivalentTo(equalsToNull.Left, topLevel: false) &&
                        assignment.Right.IsSingleLine() &&
                        !ifStatement.SpanContainsDirectives())
                    {
                        StatementSyntax fixableStatement = ifStatement;

                        SyntaxList <StatementSyntax> statements;
                        if (ifStatement.TryGetContainingList(out statements))
                        {
                            int index = statements.IndexOf(ifStatement);

                            if (index > 0)
                            {
                                StatementSyntax previousStatement = statements[index - 1];

                                if (CanRefactor(previousStatement, ifStatement, equalsToNull.Left, ifStatement.Parent))
                                {
                                    fixableStatement = previousStatement;
                                }
                            }
                        }

                        context.ReportDiagnostic(DiagnosticDescriptors.UseCoalesceExpression, fixableStatement);
                    }
                }
            }
        }
        private static ImmutableArray <IfRefactoring> Analyze(
            LocalDeclarationStatementSyntax localDeclarationStatement,
            IfStatementSyntax ifStatement,
            IfAnalysisOptions options)
        {
            VariableDeclaratorSyntax declarator = localDeclarationStatement.Declaration?.SingleVariableOrDefault();

            if (declarator != null)
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause?.Statement?.IsKind(SyntaxKind.IfStatement) == false)
                {
                    SimpleAssignmentExpression assignment1;
                    if (SimpleAssignmentExpression.TryCreate(ifStatement.GetSingleStatementOrDefault(), out assignment1))
                    {
                        SimpleAssignmentExpression assignment2;
                        if (SimpleAssignmentExpression.TryCreate(elseClause.GetSingleStatementOrDefault(), out assignment2) &&
                            assignment1.Left.IsKind(SyntaxKind.IdentifierName) &&
                            assignment2.Left.IsKind(SyntaxKind.IdentifierName))
                        {
                            string identifier1 = ((IdentifierNameSyntax)assignment1.Left).Identifier.ValueText;
                            string identifier2 = ((IdentifierNameSyntax)assignment2.Left).Identifier.ValueText;

                            if (string.Equals(identifier1, identifier2, StringComparison.Ordinal) &&
                                string.Equals(identifier1, declarator.Identifier.ValueText, StringComparison.Ordinal) &&
                                options.CheckSpanDirectives(ifStatement.Parent, TextSpan.FromBounds(localDeclarationStatement.SpanStart, ifStatement.Span.End)))
                            {
                                return(new LocalDeclarationAndIfElseAssignmentWithConditionalExpression(localDeclarationStatement, ifStatement, assignment1.Right, assignment2.Right).ToImmutableArray());
                            }
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }