private static void CheckInvocationExpression([NotNull] IInvocationExpression invocation, [NotNull] IHighlightingConsumer consumer)
        {
            var invokedExpression = invocation.InvokedExpression;

            if (invokedExpression == null)
            {
                return;
            }

            CheckInvocationInfo(invocation, invokedExpression as IReferenceExpression, consumer);

            var invocationReference = invocation.InvocationExpressionReference.NotNull("reference != null");

            var resolveResult = invocationReference.Resolve();

            if (resolveResult.ResolveErrorType != ResolveErrorType.OK)
            {
                return;
            }

            var method = resolveResult.DeclaredElement as IMethod;

            if (method == null)
            {
                return;
            }

            if (method.IsIterator)
            {
                consumer.AddHighlighting(
                    new ObjectAllocationHighlighting(invocation, "iterator method call"),
                    invokedExpression.GetExpressionRange());
            }
            else if (method.ReturnType.Classify == TypeClassification.REFERENCE_TYPE)
            {
#if RESHARPER10
                var annotationsCache = invocation.GetPsiServices().GetCodeAnnotationsCache();
                if (annotationsCache.IsPure(method) && annotationsCache.GetLinqTunnel(method))
#elif RESHARPER2016_1
                var annotationsCache             = invocation.GetPsiServices().GetCodeAnnotationsCache();
                var linqTunnelAnnotationProvider = annotationsCache.GetProvider <LinqTunnelAnnotationProvider>();
                var pureAnnotationProvider       = annotationsCache.GetProvider <PureAnnotationProvider>();

                if (pureAnnotationProvider.GetInfo(method) && linqTunnelAnnotationProvider.GetInfo(method))
#endif
                {
                    consumer.AddHighlighting(
                        new ObjectAllocationHighlighting(invocation, "LINQ method call"),
                        invokedExpression.GetExpressionRange());
                }
            }
        }
        public static bool IsInvocationExpensive([NotNull] IInvocationExpression invocationExpression)
        {
            invocationExpression.GetPsiServices().Locks.AssertReadAccessAllowed();

            var reference = (invocationExpression.InvokedExpression as IReferenceExpression)?.Reference;

            if (reference == null)
            {
                return(false);
            }

            var declaredElement = reference.Resolve().DeclaredElement as IMethod;

            return(IsInvokedElementExpensive(declaredElement));
        }
        private static string GetUniqueName([NotNull] IInvocationExpression invocationExpression, [NotNull] string baseName)
        {
            var namingManager   = invocationExpression.GetPsiServices().Naming;
            var policyProvider  = namingManager.Policy.GetPolicyProvider(invocationExpression.Language, invocationExpression.GetSourceFile());
            var namingRule      = policyProvider.GetPolicy(NamedElementKinds.PrivateStaticReadonly).NamingRule;
            var name            = namingManager.Parsing.Parse(baseName, namingRule, policyProvider);
            var nameRoot        = name.GetRootOrDefault(baseName);
            var namesCollection = namingManager.Suggestion.CreateEmptyCollection(PluralityKinds.Unknown, CSharpLanguage.Instance, true, policyProvider);

            namesCollection.Add(nameRoot, new EntryOptions(PluralityKinds.Unknown, SubrootPolicy.Decompose, emphasis: Emphasis.Good));
            var suggestionOptions = new SuggestionOptions
            {
                DefaultName       = baseName,
                UniqueNameContext = invocationExpression,
            };
            var namesSuggestion = namesCollection.Prepare(NamedElementKinds.PrivateStaticReadonly, ScopeKind.Common, suggestionOptions);

            return(namesSuggestion.FirstName());
        }
        public static bool IsInvocationExpensive([NotNull] IInvocationExpression invocationExpression)
        {
            invocationExpression.GetPsiServices().Locks.AssertReadAccessAllowed();

            var reference = (invocationExpression.InvokedExpression as IReferenceExpression)?.Reference;

            if (reference == null)
            {
                return(false);
            }

            var declaredElement = reference.Resolve().DeclaredElement as IMethod;

            var containingType = declaredElement?.GetContainingType();

            if (containingType == null)
            {
                return(false);
            }

            ISet <string> knownCostlyMethods = null;
            var           clrTypeName        = containingType.GetClrName();

            if (clrTypeName.Equals(KnownTypes.Component))
            {
                knownCostlyMethods = ourKnownComponentCostlyMethods;
            }

            if (clrTypeName.Equals(KnownTypes.MonoBehaviour))
            {
                knownCostlyMethods = ourKnownMonoBehaviourCostlyMethods;
            }

            if (clrTypeName.Equals(KnownTypes.GameObject))
            {
                knownCostlyMethods = ourKnownGameObjectCostlyMethods;
            }

            if (clrTypeName.Equals(KnownTypes.Resources))
            {
                knownCostlyMethods = ourKnownResourcesCostlyMethods;
            }

            if (clrTypeName.Equals(KnownTypes.Object))
            {
                knownCostlyMethods = ourKnownObjectCostlyMethods;
            }

            if (clrTypeName.Equals(KnownTypes.Transform))
            {
                knownCostlyMethods = ourKnownTransformCostlyMethods;
            }

            if (clrTypeName.Equals(KnownTypes.Debug))
            {
                knownCostlyMethods = ourKnownDebugCostlyMethods;
            }

            var shortName = declaredElement.ShortName;

            if (knownCostlyMethods != null && knownCostlyMethods.Contains(shortName))
            {
                return(true);
            }

            return(clrTypeName.Equals(KnownTypes.GameObject) && shortName.Equals("AddComponent"));
        }
示例#5
0
        private static void CheckInvocation(
            [NotNull] IInvocationExpression invocation, [NotNull] IHighlightingConsumer consumer)
        {
            var reference = invocation.InvocationExpressionReference;

            if (reference == null)
            {
                return;
            }

            var invokedExpression = invocation.InvokedExpression;

            var declaredElement = reference.Resolve().DeclaredElement;
            var parametersOwner = declaredElement as IParametersOwner;

            if (parametersOwner != null)
            {
                var parameters = parametersOwner.Parameters;
                if (parameters.Count > 0)
                {
                    var lastParameter = parameters[parameters.Count - 1];
                    if (lastParameter.IsParameterArray)
                    {
                        ICSharpExpression paramsArgument = null;
                        foreach (var argument in invocation.ArgumentsEnumerable)
                        {
                            var parameter = argument.MatchingParameter;
                            if (parameter != null && Equals(parameter.Element, lastParameter))
                            {
                                var parameterType = parameter.Substitution[parameter.Element.Type];
                                var convertedTo   = argument.GetImplicitlyConvertedTo();
                                if (!convertedTo.IsUnknown)
                                {
                                    if (convertedTo.Equals(parameterType))
                                    {
                                        return;
                                    }
                                    paramsArgument = argument.Value;
                                }

                                break;
                            }
                        }

                        var anchor = invokedExpression as IReferenceExpression ?? paramsArgument ?? invokedExpression;
                        consumer.AddHighlighting(
                            new ObjectAllocationHighlighting(
                                anchor, string.Format("parameters array '{0}' creation", lastParameter.ShortName)),
                            anchor.GetExpressionRange());
                    }
                }
            }

            var method = declaredElement as IMethod;

            if (method != null)
            {
                if (method.IsIterator) // todo: may be perf issue
                {
                    consumer.AddHighlighting(
                        new ObjectAllocationHighlighting(invocation, "iterator method call"),
                        invokedExpression.GetExpressionRange());
                }
                else if (method.ReturnType.Classify == TypeClassification.REFERENCE_TYPE)
                {
                    var annotationsCache = invocation.GetPsiServices().GetCodeAnnotationsCache();
                    if (annotationsCache.IsPure(method) && annotationsCache.GetLinqTunnel(method))
                    {
                        consumer.AddHighlighting(
                            new ObjectAllocationHighlighting(invocation, "LINQ method call"),
                            invokedExpression.GetExpressionRange());
                    }
                }
            }
        }