Example #1
0
        private static ImmutableArray <IfRefactoring> ToImmutableArray(IfRefactoring refactoring1, IfRefactoring refactoring2)
        {
            if (refactoring1 != null)
            {
                if (refactoring2 != null)
                {
                    return(ImmutableArray.Create(refactoring1, refactoring2));
                }
                else
                {
                    return(refactoring1.ToImmutableArray());
                }
            }
            else if (refactoring2 != null)
            {
                return(refactoring2.ToImmutableArray());
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
Example #2
0
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionStatementSyntax expressionStatement1,
            ExpressionStatementSyntax expressionStatement2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken,
            IfAnalysisOptions options)
        {
            ExpressionSyntax expression1 = expressionStatement1.Expression;

            if (IsSimpleAssignment(expression1))
            {
                ExpressionSyntax expression2 = expressionStatement2.Expression;

                if (IsSimpleAssignment(expression2))
                {
                    var assignment1 = (AssignmentExpressionSyntax)expression1;
                    var assignment2 = (AssignmentExpressionSyntax)expression2;

                    ExpressionSyntax left1  = assignment1.Left;
                    ExpressionSyntax right1 = assignment1.Right;

                    if (left1?.IsMissing == false &&
                        right1?.IsMissing == false)
                    {
                        ExpressionSyntax left2  = assignment2.Left;
                        ExpressionSyntax right2 = assignment2.Right;

                        if (left2?.IsMissing == false &&
                            right2?.IsMissing == false &&
                            SyntaxComparer.AreEquivalent(left1, left2))
                        {
                            if (options.UseCoalesceExpression)
                            {
                                NullCheckExpression nullCheck;
                                if (NullCheckExpression.TryCreate(condition, semanticModel, out nullCheck, cancellationToken))
                                {
                                    IfRefactoring refactoring = CreateIfToAssignmentWithWithCoalesceExpression(
                                        ifStatement,
                                        left1,
                                        (nullCheck.IsCheckingNull) ? right2 : right1,
                                        (nullCheck.IsCheckingNull) ? right1 : right2,
                                        nullCheck,
                                        semanticModel,
                                        cancellationToken);

                                    if (refactoring != null)
                                    {
                                        return(refactoring.ToImmutableArray());
                                    }
                                }
                            }

                            if (options.UseConditionalExpression)
                            {
                                return(new IfElseToAssignmentWithConditionalExpression(ifStatement, left1, right1, right2).ToImmutableArray());
                            }
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }