public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            if (ParseTreeResults == null)
            {
                Logger.Debug("Aborting GetInspectionResults because ParseTree results were not passed");
                return(new InspectionResultBase[] { });
            }
            var subStmts = ParseTreeResults.ArgListsWithOneByRefParam
                           .Where(context => context.Context.Parent is VBAParser.SubStmtContext)
                           .Select(context => (VBAParser.SubStmtContext)context.Context.Parent)
                           .ToList();

            var subStmtsNotImplementingInterfaces = subStmts
                                                    .Where(c =>
            {
                var declaration =
                    UserDeclarations.SingleOrDefault(d => d.Context == c);

                if (UserDeclarations.FindInterfaceMembers().Contains(declaration))
                {
                    return(false);
                }

                var interfaceImplementation = UserDeclarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));
                if (interfaceImplementation == null)
                {
                    return(true);
                }

                var interfaceMember = UserDeclarations.FindInterfaceMember(interfaceImplementation);

                return(interfaceMember == null);
            });

            var subStmtsNotImplementingEvents = subStmts
                                                .Where(c =>
            {
                var declaration = UserDeclarations.SingleOrDefault(d => d.Context == c);

                if (declaration == null)
                {
                    return(false);
                }                                               // rather be safe than sorry

                return(UserDeclarations.Where(item => item.IsWithEvents)
                       .All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null) &&
                       !State.AllDeclarations.FindBuiltInEventHandlers().Contains(declaration));
            });

            return(ParseTreeResults.ArgListsWithOneByRefParam
                   .Where(context => context.Context.Parent is VBAParser.SubStmtContext &&
                          subStmtsNotImplementingInterfaces.Contains(context.Context.Parent) &&
                          subStmtsNotImplementingEvents.Contains(context.Context.Parent))
                   .Select(context => new ProcedureShouldBeFunctionInspectionResult(this,
                                                                                    State,
                                                                                    new QualifiedContext <VBAParser.ArgListContext>(context.ModuleName,
                                                                                                                                    context.Context as VBAParser.ArgListContext),
                                                                                    new QualifiedContext <VBAParser.SubStmtContext>(context.ModuleName,
                                                                                                                                    context.Context.Parent as VBAParser.SubStmtContext))));
        }
コード例 #2
0
        public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            var subStmts = State.ArgListsWithOneByRefParam
                           .Where(context => context.Context.Parent is VBAParser.SubStmtContext)
                           .Select(context => (VBAParser.SubStmtContext)context.Context.Parent)
                           .ToList();

            var subStmtsNotImplementingInterfaces = subStmts
                                                    .Where(c =>
            {
                var declaration =
                    UserDeclarations.SingleOrDefault(d => d.DeclarationType == DeclarationType.Procedure &&
                                                     d.IdentifierName == c.identifier().GetText() &&
                                                     d.Context.GetSelection().Equals(c.GetSelection()));

                var interfaceImplementation = UserDeclarations.FindInterfaceImplementationMembers().SingleOrDefault(m => m.Equals(declaration));

                if (interfaceImplementation == null)
                {
                    return(true);
                }

                var interfaceMember = UserDeclarations.FindInterfaceMember(interfaceImplementation);
                return(interfaceMember == null);
            });

            var subStmtsNotImplementingEvents = subStmts
                                                .Where(c =>
            {
                var declaration = UserDeclarations.SingleOrDefault(d => d.DeclarationType == DeclarationType.Procedure &&
                                                                   d.IdentifierName == c.identifier().GetText() &&
                                                                   d.Context.GetSelection().Equals(c.GetSelection()));

                if (declaration == null)
                {
                    return(false);
                }                                               // rather be safe than sorry

                return(UserDeclarations.Where(item => item.IsWithEvents)
                       .All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null));
            });

            return(State.ArgListsWithOneByRefParam
                   .Where(context => context.Context.Parent is VBAParser.SubStmtContext &&
                          subStmtsNotImplementingInterfaces.Contains(context.Context.Parent) &&
                          subStmtsNotImplementingEvents.Contains(context.Context.Parent))
                   .Select(context => new ProcedureShouldBeFunctionInspectionResult(this,
                                                                                    State,
                                                                                    new QualifiedContext <VBAParser.ArgListContext>(context.ModuleName,
                                                                                                                                    context.Context as VBAParser.ArgListContext),
                                                                                    new QualifiedContext <VBAParser.SubStmtContext>(context.ModuleName,
                                                                                                                                    context.Context.Parent as VBAParser.SubStmtContext))));
        }
        public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            // Note: This inspection does not find dictionary calls (e.g. foo!bar) since we do not know what the
            // default member is of a class.
            var interfaceMembers = UserDeclarations.FindInterfaceMembers().ToList();
            var interfaceImplementationMembers = UserDeclarations.FindInterfaceImplementationMembers();
            var functions             = UserDeclarations.Where(function => function.DeclarationType == DeclarationType.Function).ToList();
            var interfaceMemberIssues = GetInterfaceMemberIssues(interfaceMembers);
            var nonInterfaceFunctions = functions.Except(interfaceMembers.Union(interfaceImplementationMembers));
            var nonInterfaceIssues    = GetNonInterfaceIssues(nonInterfaceFunctions);

            return(interfaceMemberIssues.Union(nonInterfaceIssues));
        }
コード例 #4
0
 private IEnumerable <IInspectionResult> GetInterfaceMemberIssues(IEnumerable <Declaration> interfaceMembers)
 {
     return(from interfaceMember in interfaceMembers
            let implementationMembers =
                UserDeclarations.FindInterfaceImplementationMembers(interfaceMember.IdentifierName).ToList()
                where interfaceMember.DeclarationType == DeclarationType.Function &&
                !IsReturnValueUsed(interfaceMember) &&
                implementationMembers.All(member => !IsReturnValueUsed(member))
                let implementationMemberIssues =
                    implementationMembers.Select(
                        implementationMember =>
                        Tuple.Create(implementationMember.Context,
                                     new QualifiedSelection(implementationMember.QualifiedName.QualifiedModuleName,
                                                            implementationMember.Selection), implementationMember))
                    select CreateInspectionResult(this, interfaceMember));
 }
コード例 #5
0
        protected override IEnumerable <IInspectionResult> DoGetInspectionResults()
        {
            // Note: This inspection does not find dictionary calls (e.g. foo!bar) since we do not know what the
            // default member is of a class.
            var interfaceMembers = UserDeclarations.FindInterfaceMembers().ToList();
            var interfaceImplementationMembers = UserDeclarations.FindInterfaceImplementationMembers();
            var functions = State.DeclarationFinder
                            .UserDeclarations(DeclarationType.Function)
                            .Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName))
                            .ToList();
            var interfaceMemberIssues = GetInterfaceMemberIssues(interfaceMembers);
            var nonInterfaceFunctions = functions.Except(interfaceMembers.Union(interfaceImplementationMembers));
            var nonInterfaceIssues    = GetNonInterfaceIssues(nonInterfaceFunctions);

            return(interfaceMemberIssues.Union(nonInterfaceIssues));
        }
コード例 #6
0
        public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            var interfaceMembers     = UserDeclarations.FindInterfaceImplementationMembers();
            var builtinEventHandlers = State.DeclarationFinder.FindEventHandlers();

            var issues = State.DeclarationFinder
                         .UserDeclarations(DeclarationType.Parameter)
                         .OfType <ParameterDeclaration>()
                         .Where(item => item.IsImplicitByRef && !item.IsParamArray &&
                                !IsIgnoringInspectionResultFor(item, AnnotationName) &&
                                !interfaceMembers.Contains(item.ParentDeclaration) &&
                                !builtinEventHandlers.Contains(item.ParentDeclaration))
                         .ToList();

            return(issues.Select(issue => new ImplicitByRefParameterInspectionResult(this, issue)));
        }
コード例 #7
0
        public override IEnumerable <InspectionResultBase> GetInspectionResults()
        {
            var interfaceMembers = UserDeclarations.FindInterfaceImplementationMembers();

            var issues = (from item in UserDeclarations
                          where
                          !item.IsInspectionDisabled(AnnotationName) &&
                          item.DeclarationType == DeclarationType.Parameter
                          // ParamArray parameters do not allow an explicit "ByRef" parameter mechanism.
                          && !((ParameterDeclaration)item).IsParamArray &&
                          !interfaceMembers.Select(m => m.Scope).Contains(item.ParentScope)
                          let arg = item.Context as VBAParser.ArgContext
                                    where arg != null && arg.BYREF() == null && arg.BYVAL() == null
                                    select new QualifiedContext <VBAParser.ArgContext>(item.QualifiedName, arg))
                         .Select(issue => new ImplicitByRefParameterInspectionResult(this, issue.Context.identifier().GetText(), issue));


            return(issues);
        }
コード例 #8
0
 private IEnumerable <IInspectionResult> GetInterfaceMemberIssues(IEnumerable <Declaration> interfaceMembers)
 {
     return(from interfaceMember in interfaceMembers
            let implementationMembers =
                UserDeclarations.FindInterfaceImplementationMembers(interfaceMember.IdentifierName).ToList()
                where interfaceMember.DeclarationType == DeclarationType.Function &&
                !IsReturnValueUsed(interfaceMember) &&
                implementationMembers.All(member => !IsReturnValueUsed(member))
                let implementationMemberIssues =
                    implementationMembers.Select(
                        implementationMember =>
                        Tuple.Create(implementationMember.Context,
                                     new QualifiedSelection(implementationMember.QualifiedName.QualifiedModuleName,
                                                            implementationMember.Selection), implementationMember))
                    select
                    new DeclarationInspectionResult(this,
                                                    string.Format(InspectionsUI.FunctionReturnValueNotUsedInspectionResultFormat, interfaceMember.IdentifierName),
                                                    interfaceMember, properties: new Dictionary <string, string> {
         { "DisableFixes", nameof(QuickFixes.ConvertToProcedureQuickFix) }
     }));
 }
コード例 #9
0
        protected override IEnumerable <IInspectionResult> DoGetInspectionResults()
        {
            var interfaceImplementationMembers = UserDeclarations.FindInterfaceImplementationMembers().ToHashSet();

            var excludeParameterMembers = State.DeclarationFinder.FindEventHandlers().ToHashSet();

            excludeParameterMembers.UnionWith(interfaceImplementationMembers);

            var result = UserDeclarations
                         .Where(declaration =>
                                declaration.AsTypeName == Tokens.Integer &&
                                !interfaceImplementationMembers.Contains(declaration) &&
                                declaration.DeclarationType != DeclarationType.LibraryFunction &&
                                (declaration.DeclarationType != DeclarationType.Parameter || IncludeParameterDeclaration(declaration, excludeParameterMembers)))
                         .Select(issue =>
                                 new DeclarationInspectionResult(this,
                                                                 string.Format(InspectionsUI.IntegerDataTypeInspectionResultFormat,
                                                                               RubberduckUI.ResourceManager.GetString($"DeclarationType_{issue.DeclarationType}", CultureInfo.CurrentUICulture), issue.IdentifierName),
                                                                 issue));

            return(result);
        }
コード例 #10
0
        protected override IEnumerable <IInspectionResult> DoGetInspectionResults()
        {
            var builtInEventHandlerContexts           = State.DeclarationFinder.FindEventHandlers().Select(handler => handler.Context).ToHashSet();
            var interfaceImplementationMemberContexts = UserDeclarations.FindInterfaceImplementationMembers().Select(member => member.Context).ToHashSet();

            var issues = Listener.Contexts.Where(context =>
                                                 !IsIgnoringInspectionResultFor(context.ModuleName, context.Context.Start.Line) &&
                                                 !builtInEventHandlerContexts.Contains(context.Context.Parent.Parent) &&
                                                 !interfaceImplementationMemberContexts.Contains(context.Context.Parent.Parent));

            return(issues.Select(issue =>
            {
                var identifier = ((VBAParser.ArgContext)issue.Context)
                                 .unrestrictedIdentifier()
                                 .identifier();

                return new QualifiedContextInspectionResult(this,
                                                            string.Format(InspectionsUI.ImplicitByRefModifierInspectionResultFormat,
                                                                          identifier.untypedIdentifier() != null
                            ? identifier.untypedIdentifier().identifierValue().GetText()
                            : identifier.typedIdentifier().untypedIdentifier().identifierValue().GetText()), issue);
            }));
        }