Example #1
0
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken,
            IfAnalysisOptions options,
            bool isYield)
        {
            if (expression1?.IsMissing == false &&
                expression2?.IsMissing == false)
            {
                if (options.UseCoalesceExpression)
                {
                    NullCheckExpression nullCheck;
                    if (NullCheckExpression.TryCreate(condition, semanticModel, out nullCheck, cancellationToken))
                    {
                        IfRefactoring refactoring = CreateIfToReturnWithCoalesceExpression(
                            ifStatement,
                            (nullCheck.IsCheckingNull) ? expression2 : expression1,
                            (nullCheck.IsCheckingNull) ? expression1 : expression2,
                            nullCheck,
                            isYield,
                            semanticModel,
                            cancellationToken);

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

                IfToReturnWithBooleanExpression ifToReturnWithBooleanExpression = null;

                if (options.UseBooleanExpression &&
                    (expression1.IsBooleanLiteralExpression() || expression2.IsBooleanLiteralExpression()) &&
                    semanticModel.GetTypeSymbol(expression1, cancellationToken)?.IsBoolean() == true &&
                    semanticModel.GetTypeSymbol(expression2, cancellationToken)?.IsBoolean() == true)
                {
                    ifToReturnWithBooleanExpression = IfToReturnWithBooleanExpression.Create(ifStatement, expression1, expression2, isYield);
                }

                IfToReturnWithConditionalExpression ifToReturnWithConditionalExpression = null;

                if (options.UseConditionalExpression &&
                    (!expression1.IsBooleanLiteralExpression() || !expression2.IsBooleanLiteralExpression()))
                {
                    ifToReturnWithConditionalExpression = IfToReturnWithConditionalExpression.Create(ifStatement, expression1, expression2, isYield);
                }

                return(ToImmutableArray(ifToReturnWithBooleanExpression, ifToReturnWithConditionalExpression));
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
Example #2
0
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            IfAnalysisOptions options,
            bool isYield,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            if (expression1?.IsMissing != false)
            {
                return(Empty);
            }

            if (expression2?.IsMissing != false)
            {
                return(Empty);
            }

            if (options.UseCoalesceExpression ||
                options.UseExpression)
            {
                SyntaxKind kind1 = expression1.Kind();
                SyntaxKind kind2 = expression2.Kind();

                if (kind1.IsBooleanLiteralExpression() &&
                    kind2.IsBooleanLiteralExpression() &&
                    kind1 != kind2)
                {
                    if (options.UseExpression)
                    {
                        if (ifStatement.IsSimpleIf() &&
                            (ifStatement.PreviousStatementOrDefault() is IfStatementSyntax previousIf) &&
                            previousIf.IsSimpleIf() &&
                            (previousIf.GetSingleStatementOrDefault() is ReturnStatementSyntax returnStatement) &&
                            returnStatement.Expression?.WalkDownParentheses().Kind() == kind1)
                        {
                            return(Empty);
                        }

                        return(new IfToReturnWithExpression(ifStatement, condition, isYield, negate: kind1 == SyntaxKind.FalseLiteralExpression).ToImmutableArray());
                    }

                    return(Empty);
                }

                NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(condition, semanticModel: semanticModel, cancellationToken: cancellationToken);

                if (nullCheck.Success)
                {
                    IfRefactoring refactoring = CreateIfToReturnStatement(
                        ifStatement,
                        (nullCheck.IsCheckingNull) ? expression2 : expression1,
                        (nullCheck.IsCheckingNull) ? expression1 : expression2,
                        nullCheck,
                        options,
                        isYield,
                        semanticModel,
                        cancellationToken);

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

            IfToReturnWithBooleanExpression ifToReturnWithBooleanExpression = null;

            if (options.UseBooleanExpression &&
                (expression1.Kind().IsBooleanLiteralExpression() || expression2.Kind().IsBooleanLiteralExpression()) &&
                semanticModel.GetTypeSymbol(expression1, cancellationToken)?.IsBoolean() == true &&
                semanticModel.GetTypeSymbol(expression2, cancellationToken)?.IsBoolean() == true)
            {
                ifToReturnWithBooleanExpression = IfToReturnWithBooleanExpression.Create(ifStatement, expression1, expression2, isYield);
            }

            IfToReturnWithConditionalExpression ifToReturnWithConditionalExpression = null;

            if (options.UseConditionalExpression &&
                (!expression1.Kind().IsBooleanLiteralExpression() || !expression2.Kind().IsBooleanLiteralExpression()))
            {
                ifToReturnWithConditionalExpression = IfToReturnWithConditionalExpression.Create(ifStatement, expression1, expression2, isYield);
            }

            return(ToImmutableArray(ifToReturnWithBooleanExpression, ifToReturnWithConditionalExpression));
        }
Example #3
0
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken,
            IfAnalysisOptions options,
            bool isYield)
        {
            if (expression1?.IsMissing == false &&
                expression2?.IsMissing == false)
            {
                if (options.UseCoalesceExpression)
                {
                    SyntaxKind conditionKind = condition.Kind();

                    if (conditionKind == SyntaxKind.EqualsExpression)
                    {
                        var binaryExpression = (BinaryExpressionSyntax)condition;

                        if (IsNullLiteral(binaryExpression.Right) &&
                            IsEquivalent(binaryExpression.Left, expression2))
                        {
                            return(IfToReturnWithCoalesceExpression.Create(ifStatement, expression2, expression1, isYield).ToImmutableArray());
                        }
                    }
                    else if (conditionKind == SyntaxKind.NotEqualsExpression)
                    {
                        var binaryExpression = (BinaryExpressionSyntax)condition;

                        if (IsNullLiteral(binaryExpression.Right) &&
                            IsEquivalent(binaryExpression.Left, expression1))
                        {
                            return(IfToReturnWithCoalesceExpression.Create(ifStatement, expression1, expression2, isYield).ToImmutableArray());
                        }
                    }
                }

                IfToReturnWithBooleanExpression ifToReturnWithBooleanExpression = null;

                if (options.UseBooleanExpression &&
                    semanticModel.GetTypeSymbol(expression1, cancellationToken)?.IsBoolean() == true &&
                    semanticModel.GetTypeSymbol(expression2, cancellationToken)?.IsBoolean() == true)
                {
                    ifToReturnWithBooleanExpression = IfToReturnWithBooleanExpression.Create(ifStatement, expression1, expression2, isYield);
                }

                IfToReturnWithConditionalExpression ifToReturnWithConditionalExpression = null;

                if (options.UseConditionalExpression &&
                    (!expression1.IsBooleanLiteralExpression() || !expression2.IsBooleanLiteralExpression()))
                {
                    ifToReturnWithConditionalExpression = IfToReturnWithConditionalExpression.Create(ifStatement, expression1, expression2, isYield);
                }

                return(ToImmutableArray(ifToReturnWithBooleanExpression, ifToReturnWithConditionalExpression));
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }