Ejemplo n.º 1
0
        protected override IEnumerable <IInspectionResult> DoGetInspectionResults()
        {
            var variables = State.DeclarationFinder
                            .UserDeclarations(DeclarationType.Variable)
                            .Where(d => !d.IsArray);

            var nodes = new List <IdentifierReference>();

            foreach (var variable in variables)
            {
                var parentScopeDeclaration = variable.ParentScopeDeclaration;

                if (parentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
                {
                    continue;
                }

                var tree = _walker.GenerateTree(parentScopeDeclaration.Context, variable);

                var references = tree.GetIdentifierReferences();
                // ignore set-assignments to 'Nothing'
                nodes.AddRange(references.Where(r =>
                                                !(r.Context.Parent is VBAParser.SetStmtContext setStmtContext &&
                                                  setStmtContext.expression().GetText().Equals(Tokens.Nothing))));
            }

            return(nodes
                   // Ignoring the Declaration disqualifies all assignments
                   .Where(issue => !issue.Declaration.IsIgnoringInspectionResultFor(AnnotationName))
                   .Select(issue => new IdentifierReferenceInspectionResult(this, Description, State, issue))
                   .ToList());
        }
        protected override IEnumerable <IInspectionResult> DoGetInspectionResults()
        {
            var variables = State.DeclarationFinder
                            .UserDeclarations(DeclarationType.Variable)
                            .Where(d => !d.IsArray);

            var nodes = new List <IdentifierReference>();

            foreach (var variable in variables)
            {
                var parentScopeDeclaration = variable.ParentScopeDeclaration;

                if (parentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Module))
                {
                    continue;
                }

                var tree = _walker.GenerateTree(parentScopeDeclaration.Context, variable);


                nodes.AddRange(tree.GetIdentifierReferences());
            }

            return(nodes
                   .Select(issue => new IdentifierReferenceInspectionResult(this, Description, State, issue))
                   .ToList());
        }
        private static IEnumerable <IdentifierReference> FindUnusedAssignmentReferences(Declaration localVariable, Walker walker)
        {
            if (!localVariable.References.Any(rf => rf.IsAssignment))
            {
                return(Enumerable.Empty <IdentifierReference>());
            }

            //Consider static local variables used if they are referenced anywhere within their procedure
            if (localVariable.References.Any(r => !r.IsAssignment) && IsStatic(localVariable))
            {
                return(Enumerable.Empty <IdentifierReference>());
            }

            var tree = walker.GenerateTree(localVariable.ParentScopeDeclaration.Context, localVariable);

            var allAssignmentsAndReferences = tree.Nodes(new[] { typeof(AssignmentNode), typeof(ReferenceNode) })
                                              .Where(node => localVariable.References.Contains(node.Reference))
                                              .ToList();

            var unusedAssignmentNodes = allAssignmentsAndReferences.Any(n => n is ReferenceNode)
                        ? FindUnusedAssignmentNodes(tree, localVariable, allAssignmentsAndReferences)
                        : allAssignmentsAndReferences.OfType <AssignmentNode>();

            return(unusedAssignmentNodes.Where(n => !IsDescendentOfNeverFlagNode(n))
                   .Select(n => n.Reference));
        }