Пример #1
0
        private void ConvertUsages(CSharpElementFactory factory)
        {
            foreach (var usage in myTreeNodePointers)
            {
                var node = usage.GetTreeNode() as ICSharpExpression;
                var elementAccessExpression = ElementAccessExpressionNavigator.GetByOperand(node as ICSharpExpression)
                                              .NotNull("ElementAccessExpressionNavigator.GetByOperand(node as ICSharpExpression) != null");
                var arguments = elementAccessExpression.Arguments;
                if (node == null || arguments.Count < 2)
                {
                    continue;
                }

                var newUsage = factory.CreateExpression("$0[$1][$2]", node, arguments[0], arguments[1]);
                elementAccessExpression.ReplaceBy(newUsage);
            }
        }
        public static void CreateInefficientMultidimensionalArrayAccessWarning([CanBeNull] IDeclaredElement declaredElement,
                                                                               [NotNull] IVariableDeclaration variableDeclaration, [NotNull]  IDaemonProcess daemonProcess, [NotNull]  DaemonProcessKind kind,
                                                                               [NotNull] IHighlightingConsumer consumer, [NotNull] DocumentRange highlightingRange)
        {
            if (declaredElement == null)
            {
                return;
            }

            var type = variableDeclaration.Type as IArrayType;

            if (type == null)
            {
                return;
            }

            if (type.Rank == 1)
            {
                return;
            }

            IArrayCreationExpression arrayCreationExpression = null;
            IMultipleDeclaration     multipleDeclaration     = null;

            switch (variableDeclaration)
            {
            case IFieldDeclaration fieldDeclaration:
                // perfomance optimization
                if (fieldDeclaration.GetAccessRights() != AccessRights.PRIVATE)
                {
                    return;
                }

                arrayCreationExpression = (fieldDeclaration.Initial as IExpressionInitializer)?.Value as IArrayCreationExpression;
                multipleDeclaration     = MultipleFieldDeclarationNavigator.GetByDeclarator(fieldDeclaration);

                break;

            case ILocalVariableDeclaration localVariableDeclaration:
                var initial = localVariableDeclaration.Initial;
                arrayCreationExpression = (initial as IExpressionInitializer)?.Value as IArrayCreationExpression;

                if (initial != null && arrayCreationExpression == null)
                {
                    return;
                }

                multipleDeclaration =
                    MultipleLocalVariableDeclarationNavigator.GetByDeclarator(localVariableDeclaration);
                break;

            default:
                return;
            }

            var classDeclaration = variableDeclaration.GetContainingNode <IClassLikeDeclaration>();

            if (classDeclaration == null)
            {
                return;
            }

            var usages = FindArrayUsages(declaredElement, classDeclaration, daemonProcess, kind);

            foreach (var usage in usages)
            {
                if (ElementAccessExpressionNavigator.GetByOperand(usage as ICSharpExpression) == null)
                {
                    return;
                }
            }

            if (arrayCreationExpression.ContainsErrorElement())
            {
                return;
            }

            consumer.AddHighlighting(new InefficientMultidimensionalArrayUsageWarning(usages.Select(t => t.CreateTreeElementPointer()).ToList(),
                                                                                      arrayCreationExpression, variableDeclaration, multipleDeclaration, highlightingRange));
        }