コード例 #1
0
        private static ImmutableArray <IfAnalysis> 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 (IsBooleanLiteralExpression(kind1) &&
                    IsBooleanLiteralExpression(kind2) &&
                    kind1 != kind2)
                {
                    if (options.UseExpression)
                    {
                        if (ifStatement.IsSimpleIf() &&
                            (ifStatement.PreviousStatement() is IfStatementSyntax previousIf) &&
                            previousIf.IsSimpleIf() &&
                            (previousIf.SingleNonBlockStatementOrDefault() is ReturnStatementSyntax returnStatement) &&
                            returnStatement.Expression?.WalkDownParentheses().Kind() == kind1)
                        {
                            return(Empty);
                        }

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

                    return(Empty);
                }

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

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

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

            IfToReturnWithBooleanExpressionAnalysis ifToReturnWithBooleanExpression = null;

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

            IfToReturnWithConditionalExpressionAnalysis ifToReturnWithConditionalExpression = null;

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

            return(ToImmutableArray(ifToReturnWithBooleanExpression, ifToReturnWithConditionalExpression));
        }