コード例 #1
0
        void AnalyzeIteratorMethod([NotNull] IHighlightingConsumer consumer, [NotNull] IAttributesOwnerDeclaration attributesOwnerDeclaration)
        {
            Debug.Assert(!attributesOwnerDeclaration.IsNullableAnnotationsContextEnabled());

            foreach (var attributeMark in GetAttributeMarks(attributesOwnerDeclaration))
            {
                if (attributeMark != null)
                {
                    if (attributeMark.AnnotationNullableValue == CodeAnnotationNullableValue.CAN_BE_NULL)
                    {
                        consumer.AddHighlighting(
                            new NotAllowedAnnotationWarning(
                                attributesOwnerDeclaration,
                                attributeMark.Attribute,
                                "Annotation is not valid because the declared element can never be null by default."));
                    }
                }
                else
                {
                    var nonNullAnnotationAttributeType = codeAnnotationsConfiguration.GetAttributeTypeForElement(
                        attributesOwnerDeclaration,
                        NullnessProvider.NotNullAttributeShortName);
                    if (nonNullAnnotationAttributeType != null)
                    {
                        consumer.AddHighlighting(
                            new MissingAnnotationWarning(
                                string.Format(
                                    "Declared element can never be null by default, but is not annotated with '{0}'.",
                                    NullnessProvider.NotNullAttributeShortName),
                                attributesOwnerDeclaration));
                    }
                    break;
                }
            }
        }
コード例 #2
0
        static void AnalyzeOther(
            ValueAnalysisMode valueAnalysisMode,
            [NotNull] IHighlightingConsumer consumer,
            [NotNull] NullnessProvider nullnessProvider,
            [NotNull] CodeAnnotationsConfiguration codeAnnotationsConfiguration,
            [NotNull] IAttributesOwnerDeclaration attributesOwnerDeclaration)
        {
            switch (valueAnalysisMode)
            {
            case ValueAnalysisMode.OPTIMISTIC:
                foreach (var attributeMark in GetAttributeMarks(nullnessProvider, attributesOwnerDeclaration))
                {
                    if (attributeMark == null)
                    {
                        var nonNullAnnotationAttributeType = codeAnnotationsConfiguration.GetAttributeTypeForElement(
                            attributesOwnerDeclaration,
                            NullnessProvider.NotNullAttributeShortName);
                        var canBeNullAnnotationAttributeType = codeAnnotationsConfiguration.GetAttributeTypeForElement(
                            attributesOwnerDeclaration,
                            NullnessProvider.CanBeNullAttributeShortName);
                        if (nonNullAnnotationAttributeType != null || canBeNullAnnotationAttributeType != null)
                        {
                            consumer.AddHighlighting(
                                new MissingAnnotationHighlighting(
                                    string.Format(
                                        @"Declared element is nullable, but is not annotated with '{0}' or '{1}'.",
                                        NullnessProvider.NotNullAttributeShortName,
                                        NullnessProvider.CanBeNullAttributeShortName),
                                    attributesOwnerDeclaration));
                        }
                        break;
                    }
                }
                break;

            case ValueAnalysisMode.PESSIMISTIC:
                foreach (var attributeMark in GetAttributeMarks(nullnessProvider, attributesOwnerDeclaration))
                {
                    if (attributeMark != null && attributeMark.AnnotationNullableValue == CodeAnnotationNullableValue.CAN_BE_NULL)
                    {
                        consumer.AddHighlighting(
                            new RedundantAnnotationHighlighting(
                                attributesOwnerDeclaration,
                                attributeMark.Attribute,
                                "Annotation is redundant because the declared element can be null by default."));
                    }
                }
                break;
            }
        }