Exemplo n.º 1
0
        static void AnalyzeAssertions(
            [NotNull] ElementProblemAnalyzerData data,
            [NotNull] IHighlightingConsumer consumer,
            [NotNull] ICSharpTreeNode rootNode,
            [NotNull] ICSharpControlFlowGraph controlFlowGraph)
        {
            var codeAnnotationsCache = rootNode.GetPsiServices().GetCodeAnnotationsCache();

            var nullnessProvider = codeAnnotationsCache.GetProvider <NullnessProvider>();
            var assertionMethodAnnotationProvider    = codeAnnotationsCache.GetProvider <AssertionMethodAnnotationProvider>();
            var assertionConditionAnnotationProvider = codeAnnotationsCache.GetProvider <AssertionConditionAnnotationProvider>();

            var assertions = Assertion.CollectAssertions(assertionMethodAnnotationProvider, assertionConditionAnnotationProvider, rootNode);

            assertions.ExceptWith(
                from highlightingInfo in consumer.Highlightings
                where highlightingInfo != null
                let redundantAssertionHighlighting = highlightingInfo.Highlighting as RedundantAssertionHighlighting
                                                     where redundantAssertionHighlighting != null
                                                     select redundantAssertionHighlighting.Assertion);

            if (assertions.Count == 0)
            {
                return; // no (new) assertions found
            }

            var inspector = CSharpControlFlowGraphInspector.Inspect(controlFlowGraph, data.GetValueAnalysisMode());

            var alwaysSuccessTryCastExpressions =
                new HashSet <IAsExpression>(inspector.AlwaysSuccessTryCastExpressions ?? Array.Empty <IAsExpression>());

            foreach (var assertion in assertions)
            {
                switch (assertion.AssertionConditionType)
                {
                case AssertionConditionType.IS_TRUE:
                    AnalyzeWhenExpressionIsKnownToBeTrueOrFalse(
                        consumer,
                        nullnessProvider,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        true);
                    break;

                case AssertionConditionType.IS_FALSE:
                    AnalyzeWhenExpressionIsKnownToBeTrueOrFalse(
                        consumer,
                        nullnessProvider,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        false);
                    break;

                case AssertionConditionType.IS_NOT_NULL:
                    AnalyzeWhenExpressionIsKnownToBeNullOrNotNull(
                        consumer,
                        nullnessProvider,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        false);
                    break;

                case AssertionConditionType.IS_NULL:
                    AnalyzeWhenExpressionIsKnownToBeNullOrNotNull(
                        consumer,
                        nullnessProvider,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        true);
                    break;
                }
            }
        }
        void AnalyzeAssertions(
            [NotNull] ElementProblemAnalyzerData data,
            [NotNull] IHighlightingConsumer consumer,
            [NotNull] ICSharpTreeNode rootNode,
            [NotNull] ICSharpControlFlowGraph controlFlowGraph)
        {
            var assertions = Assertion.CollectAssertions(assertionMethodAnnotationProvider, assertionConditionAnnotationProvider, rootNode);

            assertions.ExceptWith(
                from highlightingInfo in consumer.Highlightings
                where highlightingInfo != null
                let redundantAssertionHighlighting = highlightingInfo.Highlighting as RedundantAssertionSuggestion
                                                     where redundantAssertionHighlighting != null
                                                     select redundantAssertionHighlighting.Assertion);

            if (assertions.Count == 0)
            {
                return; // no (new) assertions found
            }

            CSharpCompilerNullableInspector nullabilityInspector;
            CSharpControlFlowGraphInspector inspector;
            HashSet <IAsExpression>         alwaysSuccessTryCastExpressions;

            if (rootNode.IsNullableWarningsContextEnabled())
            {
                nullabilityInspector = (CSharpCompilerNullableInspector)CSharpCompilerNullableInspector.Inspect(
                    controlFlowGraph,
                    null,
                    ValueAnalysisMode.OFF); // wrong [NotNull] annotation in R# code
                inspector = null;
                alwaysSuccessTryCastExpressions = null;
            }
            else
            {
                nullabilityInspector            = null;
                inspector                       = CSharpControlFlowGraphInspector.Inspect(controlFlowGraph, data.GetValueAnalysisMode());
                alwaysSuccessTryCastExpressions =
                    new HashSet <IAsExpression>(inspector.AlwaysSuccessTryCastExpressions ?? Array.Empty <IAsExpression>());
            }

            foreach (var assertion in assertions)
            {
                switch (assertion.AssertionConditionType)
                {
                case AssertionConditionType.IS_TRUE:
                    AnalyzeWhenExpressionIsKnownToBeTrueOrFalse(
                        consumer,
                        nullabilityInspector,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        true);
                    break;

                case AssertionConditionType.IS_FALSE:
                    AnalyzeWhenExpressionIsKnownToBeTrueOrFalse(
                        consumer,
                        nullabilityInspector,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        false);
                    break;

                case AssertionConditionType.IS_NOT_NULL:
                    AnalyzeWhenExpressionIsKnownToBeNullOrNotNull(
                        consumer,
                        nullabilityInspector,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        false);
                    break;

                case AssertionConditionType.IS_NULL:
                    AnalyzeWhenExpressionIsKnownToBeNullOrNotNull(
                        consumer,
                        nullabilityInspector,
                        inspector,
                        alwaysSuccessTryCastExpressions,
                        assertion,
                        true);
                    break;
                }
            }
        }