예제 #1
0
        static CSharpControlFlowNullReferenceState GetExpressionNullReferenceState(
            [NotNull] NullnessProvider nullnessProvider,
            [NotNull] CSharpControlFlowGraphInspector inspector,
            [NotNull] HashSet <IAsExpression> alwaysSuccessTryCastExpressions,
            ICSharpExpression expression)
        {
            switch (expression)
            {
            case IReferenceExpression referenceExpression:
                var nullReferenceState = inspector.GetExpressionNullReferenceState(referenceExpression, true);

                if (nullReferenceState == CSharpControlFlowNullReferenceState.UNKNOWN)
                {
                    return(GetExpressionNullReferenceStateByAnnotations(nullnessProvider, referenceExpression));
                }

                return(nullReferenceState);

            case IAsExpression asExpression when alwaysSuccessTryCastExpressions.Contains(asExpression):
                return(CSharpControlFlowNullReferenceState.NOT_NULL);

            case IObjectCreationExpression _: return(CSharpControlFlowNullReferenceState.NOT_NULL);

            case IInvocationExpression invocationExpression:
                if (invocationExpression.InvokedExpression is IReferenceExpression invokedExpression)
                {
                    return(GetExpressionNullReferenceStateByAnnotations(nullnessProvider, invokedExpression));
                }

                goto default;

            default: return(CSharpControlFlowNullReferenceState.UNKNOWN);
            }
        }
        CSharpControlFlowNullReferenceState GetExpressionNullReferenceState(
            [CanBeNull] CSharpCompilerNullableInspector nullabilityInspector,
            [CanBeNull] CSharpControlFlowGraphInspector inspector,
            [CanBeNull][ItemNotNull] HashSet <IAsExpression> alwaysSuccessTryCastExpressions,
            [NotNull] ICSharpExpression expression)
        {
            if (nullabilityInspector != null)
            {
                return(GetExpressionNullReferenceStateByNullableContext(nullabilityInspector, expression));
            }

            Debug.Assert(inspector != null);
            Debug.Assert(alwaysSuccessTryCastExpressions != null);

            while (true)
            {
                switch (expression)
                {
                case IReferenceExpression referenceExpression:
                    if (referenceExpression is IConditionalAccessExpression conditionalAccessExpression &&
                        conditionalAccessExpression.HasConditionalAccessSign)
                    {
                        var referenceState = GetExpressionNullReferenceStateByAnnotations(referenceExpression);
                        if (referenceState == CSharpControlFlowNullReferenceState.NOT_NULL)
                        {
                            expression = conditionalAccessExpression.ConditionalQualifier;
                            continue;
                        }
                    }

                    var nullReferenceState = inspector.GetExpressionNullReferenceState(referenceExpression, true);

                    if (nullReferenceState == CSharpControlFlowNullReferenceState.UNKNOWN)
                    {
                        nullReferenceState = GetExpressionNullReferenceStateByAnnotations(referenceExpression);
                    }

                    return(nullReferenceState);

                case IAsExpression asExpression when alwaysSuccessTryCastExpressions.Contains(asExpression):
                    return(CSharpControlFlowNullReferenceState.NOT_NULL);

                case IObjectCreationExpression _: return(CSharpControlFlowNullReferenceState.NOT_NULL);

                case IInvocationExpression invocationExpression:
                    if (invocationExpression.InvokedExpression is IReferenceExpression invokedExpression)
                    {
                        return(GetExpressionNullReferenceStateByAnnotations(invokedExpression));
                    }

                    goto default;

                default: return(CSharpControlFlowNullReferenceState.UNKNOWN);
                }
            }
        }