Exemplo n.º 1
0
        private static void CheckReferenceExpression(
            [NotNull] IReferenceExpression referenceExpression, [NotNull] IHighlightingConsumer consumer)
        {
            var declaredElement = referenceExpression.Reference.Resolve().DeclaredElement;

            var property = declaredElement as IProperty;

            if (property != null && property.Getter != null && property.Getter.IsIterator)
            {
                var service    = referenceExpression.Language.LanguageServiceNotNull();
                var accessType = service.GetReferenceAccessType(referenceExpression.Reference);
                if (accessType == ReferenceAccessType.READ)
                {
                    consumer.AddHighlighting(
                        new ObjectAllocationHighlighting(referenceExpression, "iterator property access"),
                        referenceExpression.NameIdentifier.GetDocumentRange());
                }
            }

            if (declaredElement is IMethod)
            {
                var type = referenceExpression.GetImplicitlyConvertedTo() as IDeclaredType;
                if (type != null && !type.IsUnknown && type.GetTypeElement() is IDelegate)
                {
                    consumer.AddHighlighting(
                        new DelegateAllocationHighlighting(referenceExpression, "from method group"),
                        referenceExpression.NameIdentifier.GetDocumentRange());
                }
            }
        }
Exemplo n.º 2
0
        private static void CheckReferenceExpression([NotNull] IReferenceExpression referenceExpression, [NotNull] IHighlightingConsumer consumer)
        {
            var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression);

            if (invocationExpression != null)
            {
                return;
            }

            var resolveResult = referenceExpression.Reference.Resolve();

            if (!resolveResult.ResolveErrorType.IsAcceptable)
            {
                return;
            }

            var method = resolveResult.DeclaredElement as IMethod;

            if (method == null || method.IsStatic || method.IsExtensionMethod)
            {
                return;
            }

            var qualifierType = GetQualifierExpressionType(referenceExpression.QualifierExpression, referenceExpression);

            if (qualifierType == null)
            {
                return;
            }

            var isValueType = qualifierType.IsValueType();

            if (!isValueType && !qualifierType.IsUnconstrainedGenericType())
            {
                return;
            }

            var targetType = referenceExpression.GetImplicitlyConvertedTo(); // delayed

            if (!targetType.IsDelegateType())
            {
                return;
            }

            var description = BakeDescriptionWithTypes(
                "conversion of value type '{0}' instance method to '{1}' delegate type", qualifierType, targetType);

            var nameIdentifier = referenceExpression.NameIdentifier;

            if (isValueType)
            {
                consumer.AddHighlighting(
                    new BoxingAllocationHighlighting(nameIdentifier, description), nameIdentifier.GetDocumentRange());
            }
            else
            {
                consumer.AddHighlighting(
                    new BoxingAllocationPossibleHighlighting(nameIdentifier, description), nameIdentifier.GetDocumentRange());
            }
        }
Exemplo n.º 3
0
        private static void CheckStructMethodConversionToDelegateInstance(
            [NotNull] IReferenceExpression referenceExpression, [NotNull] IHighlightingConsumer consumer)
        {
            var invocationExpression = InvocationExpressionNavigator.GetByInvokedExpression(referenceExpression);

            if (invocationExpression != null)
            {
                return;                         // also filters out 'nameof(o.M)'
            }
            var(declaredElement, _, resolveErrorType) = referenceExpression.Reference.Resolve();
            if (!resolveErrorType.IsAcceptable)
            {
                return;
            }

            var method = declaredElement as IMethod;

            if (method == null || method.IsStatic)
            {
                return;
            }

            var qualifierType = TryGetQualifierExpressionType(referenceExpression);

            if (qualifierType == null)
            {
                return;
            }

            var targetType = referenceExpression.GetImplicitlyConvertedTo();

            if (!targetType.IsDelegateType())
            {
                return;
            }

            var qualifierTypeKind = IsQualifierOfValueType(qualifierType, includeStructTypeParameters: true);

            if (qualifierTypeKind == Classification.Not)
            {
                return;
            }

            var description = BakeDescriptionWithTypes(
                "conversion of value type '{0}' instance method to '{1}' delegate type", qualifierType, targetType);

            if (qualifierTypeKind == Classification.Definitely)
            {
                consumer.AddHighlighting(
                    new BoxingAllocationHighlighting(referenceExpression.NameIdentifier, description));
            }
            else
            {
                consumer.AddHighlighting(
                    new PossibleBoxingAllocationHighlighting(referenceExpression.NameIdentifier, description));
            }
        }