public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var interfaceMemberScopes = declarations.FindInterfaceMembers().Select(m => m.Scope).ToList(); var interfaceImplementationMemberScopes = declarations.FindInterfaceImplementationMembers().Select(m => m.Scope).ToList(); var builtInHandlers = declarations.FindBuiltInEventHandlers(); var parameters = declarations.Where(parameter => parameter.DeclarationType == DeclarationType.Parameter && !(parameter.Context.Parent.Parent is VBAParser.EventStmtContext) && !(parameter.Context.Parent.Parent is VBAParser.DeclareStmtContext)); var unused = parameters.Where(parameter => !parameter.References.Any()).ToList(); var editor = new ActiveCodePaneEditor(_vbe, _wrapperFactory); var quickFixRefactoring = new RemoveParametersRefactoring( new RemoveParametersPresenterFactory(editor, new RemoveParametersDialog(), State, _messageBox), editor); var issues = from issue in unused.Where(parameter => !IsInterfaceMemberParameter(parameter, interfaceMemberScopes) && !builtInHandlers.Contains(parameter.ParentDeclaration)) let isInterfaceImplementationMember = IsInterfaceMemberImplementationParameter(issue, interfaceImplementationMemberScopes) select new ParameterNotUsedInspectionResult(this, issue, ((dynamic)issue.Context).identifier(), issue.QualifiedName, isInterfaceImplementationMember, quickFixRefactoring, State); return(issues.ToList()); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var interfaceMemberScopes = declarations.FindInterfaceMembers().Select(m => m.Scope).ToList(); var interfaceImplementationMemberScopes = declarations.FindInterfaceImplementationMembers().Select(m => m.Scope).ToList(); var builtInHandlers = State.AllDeclarations.FindBuiltInEventHandlers(); var parameters = declarations.Where(parameter => parameter.DeclarationType == DeclarationType.Parameter && parameter.ParentDeclaration.DeclarationType != DeclarationType.Event && parameter.ParentDeclaration.DeclarationType != DeclarationType.LibraryFunction && parameter.ParentDeclaration.DeclarationType != DeclarationType.LibraryProcedure); var unused = parameters.Where(parameter => !parameter.References.Any()).ToList(); var issues = from issue in unused.Where(parameter => !IsInterfaceMemberParameter(parameter, interfaceMemberScopes) && !builtInHandlers.Contains(parameter.ParentDeclaration)) let isInterfaceImplementationMember = IsInterfaceMemberImplementationParameter(issue, interfaceImplementationMemberScopes) select new ParameterNotUsedInspectionResult(this, issue, ((dynamic)issue.Context).unrestrictedIdentifier(), issue.QualifiedName, isInterfaceImplementationMember, issue.Project.VBE, State, _messageBox); return(issues.ToList()); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { if (ParseTreeResults == null) { Logger.Debug("Aborting GetInspectionResults because ParseTree results were not passed"); return(new InspectionResultBase[] { }); } var userDeclarations = UserDeclarations.ToList(); var builtinHandlers = State.DeclarationFinder.FindEventHandlers().ToList(); var contextLookup = userDeclarations.Where(decl => decl.Context != null).ToDictionary(decl => decl.Context); var ignored = new HashSet <Declaration>(State.DeclarationFinder.FindAllInterfaceMembers() .Concat(State.DeclarationFinder.FindAllInterfaceImplementingMembers()) .Concat(builtinHandlers) .Concat(userDeclarations.Where(item => item.IsWithEvents))); return(ParseTreeResults.Where(context => context.Context.Parent is VBAParser.SubStmtContext) .Select(context => contextLookup[(VBAParser.SubStmtContext)context.Context.Parent]) .Where(decl => !IsIgnoringInspectionResultFor(decl, AnnotationName) && !ignored.Contains(decl) && userDeclarations.Where(item => item.IsWithEvents) .All(withEvents => userDeclarations.FindEventProcedures(withEvents) == null) && !builtinHandlers.Contains(decl)) .Select(result => new ProcedureCanBeWrittenAsFunctionInspectionResult( this, State, new QualifiedContext <VBAParser.ArgListContext>(result.QualifiedName, result.Context.GetChild <VBAParser.ArgListContext>(0)), new QualifiedContext <VBAParser.SubStmtContext>(result.QualifiedName, (VBAParser.SubStmtContext)result.Context)) )); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var classes = declarations.Where(item => item.DeclarationType == DeclarationType.ClassModule).ToList(); var modules = declarations.Where(item => item.DeclarationType == DeclarationType.ProceduralModule).ToList(); var handlers = declarations.Where(item => item.DeclarationType == DeclarationType.Control) .SelectMany(control => declarations.FindEventHandlers(control)).ToList(); var withEventFields = declarations.Where(item => item.DeclarationType == DeclarationType.Variable && item.IsWithEvents); handlers.AddRange(withEventFields.SelectMany(field => declarations.FindEventProcedures(field))); var forms = declarations.Where(item => item.DeclarationType == DeclarationType.ClassModule && item.QualifiedName.QualifiedModuleName.Component.Type == vbext_ComponentType.vbext_ct_MSForm) .ToList(); if (forms.Any()) { handlers.AddRange(forms.SelectMany(form => declarations.FindFormEventHandlers(form))); } var items = declarations .Where(item => !IsIgnoredDeclaration(declarations, item, handlers, classes, modules) && !item.IsInspectionDisabled(AnnotationName)).ToList(); var issues = items.Select(issue => new IdentifierNotUsedInspectionResult(this, issue, issue.Context, issue.QualifiedName.QualifiedModuleName)); issues = DocumentNames.DocumentEventHandlerPrefixes.Aggregate(issues, (current, item) => current.Where(issue => !issue.Description.Contains("'" + item))); return(issues.ToList()); }
protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { var declarations = UserDeclarations.ToList(); var interfaceMembers = State.DeclarationFinder.FindAllInterfaceMembers(); var functions = declarations .Where(declaration => ReturningMemberTypes.Contains(declaration.DeclarationType) && !interfaceMembers.Contains(declaration)).ToList(); var unassigned = (from function in functions let isUdt = IsReturningUserDefinedType(function) let inScopeRefs = function.References.Where(r => r.ParentScoping.Equals(function)) where (!isUdt && (!inScopeRefs.Any(r => r.IsAssignment) && !inScopeRefs.Any(reference => IsAssignedByRefArgument(function, reference)))) || (isUdt && !IsUserDefinedTypeAssigned(function)) select function) .ToList(); return(unassigned .Select(issue => new DeclarationInspectionResult(this, string.Format(InspectionResults.NonReturningFunctionInspection, issue.IdentifierName), issue))); }
protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { var userDeclarations = UserDeclarations.ToList(); var builtinHandlers = State.DeclarationFinder.FindEventHandlers().ToList(); var contextLookup = userDeclarations.Where(decl => decl.Context != null).ToDictionary(decl => decl.Context); var ignored = new HashSet <Declaration>(State.DeclarationFinder.FindAllInterfaceMembers() .Concat(State.DeclarationFinder.FindAllInterfaceImplementingMembers()) .Concat(builtinHandlers) .Concat(userDeclarations.Where(item => item.IsWithEvents))); return(Listener.Contexts .Where(context => context.Context.Parent is VBAParser.SubStmtContext && contextLookup[context.Context.GetChild <VBAParser.ArgContext>()].References .Any(reference => reference.IsAssignment)) .Select(context => contextLookup[(VBAParser.SubStmtContext)context.Context.Parent]) .Where(decl => !IsIgnoringInspectionResultFor(decl, AnnotationName) && !ignored.Contains(decl) && userDeclarations.Where(item => item.IsWithEvents) .All(withEvents => userDeclarations.FindEventProcedures(withEvents) == null) && !builtinHandlers.Contains(decl)) .Select(result => new DeclarationInspectionResult(this, string.Format(InspectionResults.ProcedureCanBeWrittenAsFunctionInspection, result.IdentifierName), result))); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var results = UserDeclarations.ToList(); var declarations = from item in results where item.HasTypeHint() // bug: this inspection result only has one value. Why are we passing two in? select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.NonReturningFunctionInspectionResultFormat, InspectionsUI.Inspections_DeclarationOf + item.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedName, item.Context), item); // todo: localize this InspectionResultFormat properly var references = from item in results.SelectMany(d => d.References) where item.HasTypeHint() select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.NonReturningFunctionInspectionResultFormat, InspectionsUI.Inspections_UsageOf + item.Declaration.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedModuleName, item.Context), item.Declaration); return(declarations.Union(references)); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var interfaceMembers = declarations.FindInterfaceMembers(); var interfaceImplementationMembers = declarations.FindInterfaceImplementationMembers(); var functions = declarations .Where(declaration => ReturningMemberTypes.Contains(declaration.DeclarationType) && !interfaceMembers.Contains(declaration)).ToList(); return(functions .Where(declaration => declaration.References.All(r => !r.IsAssignment)) .Select(issue => new NonReturningFunctionInspectionResult(this, new QualifiedContext <ParserRuleContext>(issue.QualifiedName, issue.Context), interfaceImplementationMembers.Select(m => m.Scope).Contains(issue.Scope), issue))); }
protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { var declarations = UserDeclarations.ToList(); var classes = State.DeclarationFinder.UserDeclarations(DeclarationType.ClassModule) .Concat(State.DeclarationFinder.UserDeclarations(DeclarationType.Document)) .ToList(); var modules = State.DeclarationFinder.UserDeclarations(DeclarationType.ProceduralModule).ToList(); var handlers = State.DeclarationFinder.UserDeclarations(DeclarationType.Control) .SelectMany(control => declarations.FindEventHandlers(control)).ToList(); var builtInHandlers = State.DeclarationFinder.FindEventHandlers(); handlers.AddRange(builtInHandlers); var withEventFields = State.DeclarationFinder.UserDeclarations(DeclarationType.Variable).Where(item => item.IsWithEvents).ToList(); var withHanders = withEventFields .SelectMany(field => State.DeclarationFinder.FindHandlersForWithEventsField(field)) .ToList(); handlers.AddRange(withHanders); var forms = State.DeclarationFinder.UserDeclarations(DeclarationType.ClassModule) .Where(item => item.QualifiedName.QualifiedModuleName.ComponentType == ComponentType.UserForm) .ToList(); if (forms.Any()) { handlers.AddRange(forms.SelectMany(form => State.FindFormEventHandlers(form))); } var interfaceMembers = State.DeclarationFinder.FindAllInterfaceMembers().ToList(); var implementingMembers = State.DeclarationFinder.FindAllInterfaceImplementingMembers().ToList(); var items = declarations .Where(item => !IsIgnoredDeclaration(item, interfaceMembers, implementingMembers, handlers, classes, modules)).ToList(); var issues = items.Select(issue => new DeclarationInspectionResult(this, string.Format(InspectionResults.IdentifierNotUsedInspection, issue.DeclarationType.ToLocalizedString(), issue.IdentifierName), issue)); issues = DocumentEventHandlerPrefixes .Aggregate(issues, (current, item) => current.Where(issue => !issue.Description.Contains($"'{item}"))); return(issues.ToList()); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var results = UserDeclarations.ToList(); var options = results .Where(declaration => declaration.DeclarationType == DeclarationType.ModuleOption && declaration.Context is VBAParser.OptionExplicitStmtContext) .ToList(); var modules = results .Where(declaration => ModuleTypes.Contains(declaration.DeclarationType)); var issues = modules.Where(module => !options.Select(option => option.Scope).Contains(module.Scope)) .Select(issue => new OptionExplicitInspectionResult(this, issue)); return(issues); }
protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { if (!Listener.Contexts.Any()) { return(Enumerable.Empty <IInspectionResult>()); } var userDeclarations = UserDeclarations.ToList(); var builtinHandlers = State.DeclarationFinder.FindEventHandlers().ToList(); var contextLookup = userDeclarations.Where(decl => decl.Context != null).ToDictionary(decl => decl.Context); var ignored = new HashSet <Declaration>(State.DeclarationFinder.FindAllInterfaceMembers() .Concat(State.DeclarationFinder.FindAllInterfaceImplementingMembers()) .Concat(builtinHandlers) .Concat(userDeclarations.Where(item => item.IsWithEvents))); return(Listener.Contexts .Where(context => context.Context.Parent is VBAParser.SubStmtContext && HasArgumentReferencesWithIsAssignmentFlagged(context)) .Select(context => GetSubStmtParentDeclaration(context)) .Where(decl => decl != null && !IsIgnoringInspectionResultFor(decl, AnnotationName) && !ignored.Contains(decl) && userDeclarations.Where(item => item.IsWithEvents) .All(withEvents => userDeclarations.FindEventProcedures(withEvents) == null) && !builtinHandlers.Contains(decl)) .Select(result => new DeclarationInspectionResult(this, string.Format(InspectionResults.ProcedureCanBeWrittenAsFunctionInspection, result.IdentifierName), result))); bool HasArgumentReferencesWithIsAssignmentFlagged(QualifiedContext <ParserRuleContext> context) { return(contextLookup.TryGetValue(context.Context.GetChild <VBAParser.ArgContext>(), out Declaration decl) ? decl.References.Any(rf => rf.IsAssignment) : false); } Declaration GetSubStmtParentDeclaration(QualifiedContext <ParserRuleContext> context) { return(contextLookup.TryGetValue(context.Context.Parent as VBAParser.SubStmtContext, out Declaration decl) ? decl : null); } }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var setters = declarations .Where(item => (item.Accessibility == Accessibility.Implicit || item.Accessibility == Accessibility.Public || item.Accessibility == Accessibility.Global) && (item.DeclarationType == DeclarationType.PropertyLet || item.DeclarationType == DeclarationType.PropertySet) && !declarations.Where(declaration => declaration.IdentifierName == item.IdentifierName) .Any(accessor => !accessor.IsBuiltIn && accessor.DeclarationType == DeclarationType.PropertyGet)) .GroupBy(item => new { item.QualifiedName, item.DeclarationType }) .Select(grouping => grouping.First()); // don't get both Let and Set accessors return(setters.Select(setter => new WriteOnlyPropertyInspectionResult(this, setter))); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var items = UserDeclarations.ToList(); // ignore arrays. todo: ArrayIndicesNotAccessedInspection var arrays = items.Where(declaration => declaration.DeclarationType == DeclarationType.Variable && declaration.IsArray).ToList(); var declarations = items.Except(arrays).Where(declaration => declaration.DeclarationType == DeclarationType.Variable && !declaration.IsWithEvents && !items.Any(item => item.IdentifierName == declaration.AsTypeName && item.DeclarationType == DeclarationType.UserDefinedType) && // UDT variables don't need to be assigned !declaration.IsSelfAssigned && !declaration.References.Any(reference => reference.IsAssignment)); return(declarations.Select(issue => new IdentifierNotAssignedInspectionResult(this, issue, issue.Context))); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var issues = new List <ParameterCanBeByValInspectionResult>(); var interfaceDeclarationMembers = declarations.FindInterfaceMembers().ToList(); var interfaceScopes = declarations.FindInterfaceImplementationMembers().Concat(interfaceDeclarationMembers).Select(s => s.Scope); issues.AddRange(GetResults(declarations, interfaceDeclarationMembers)); var eventMembers = declarations.Where(item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Event).ToList(); var formEventHandlerScopes = State.FindFormEventHandlers().Select(handler => handler.Scope); var eventHandlerScopes = State.AllDeclarations.FindBuiltInEventHandlers().Concat(declarations.FindUserEventHandlers()).Select(e => e.Scope); var eventScopes = eventMembers.Select(s => s.Scope) .Concat(formEventHandlerScopes) .Concat(eventHandlerScopes); issues.AddRange(GetResults(declarations, eventMembers)); var declareScopes = declarations.Where(item => item.DeclarationType == DeclarationType.LibraryFunction || item.DeclarationType == DeclarationType.LibraryProcedure) .Select(e => e.Scope); issues.AddRange(declarations.Where(declaration => !declaration.IsArray && (declaration.AsTypeDeclaration == null || declaration.AsTypeDeclaration.DeclarationType != DeclarationType.UserDefinedType) && !declareScopes.Contains(declaration.ParentScope) && !eventScopes.Contains(declaration.ParentScope) && !interfaceScopes.Contains(declaration.ParentScope) && declaration.DeclarationType == DeclarationType.Parameter && ((VBAParser.ArgContext)declaration.Context).BYVAL() == null && !IsUsedAsByRefParam(declarations, declaration) && !declaration.References.Any(reference => reference.IsAssignment)) .Select(issue => new ParameterCanBeByValInspectionResult(this, State, issue, issue.Context, issue.QualifiedName))); return(issues); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); var interfaceMembers = declarations.FindInterfaceMembers(); var interfaceImplementationMembers = declarations.FindInterfaceImplementationMembers(); var functions = declarations .Where(declaration => ReturningMemberTypes.Contains(declaration.DeclarationType) && !interfaceMembers.Contains(declaration)).ToList(); var unassigned = from function in functions let isUdt = IsReturningUserDefinedType(function) let inScopeRefs = function.References.Where(r => r.ParentScoping.Equals(function)) where (!isUdt && (!inScopeRefs.Any(r => r.IsAssignment))) || (isUdt && !IsUserDefinedTypeAssigned(function)) select function; return(unassigned .Select(issue => new NonReturningFunctionInspectionResult(this, new QualifiedContext <ParserRuleContext>(issue.QualifiedName, issue.Context), issue, canConvertToProcedure: !IsRecursive(issue) && !interfaceImplementationMembers.Select(m => m.Scope).Contains(issue.Scope)))); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var results = UserDeclarations.ToList(); var declarations = from item in results where item.HasTypeHint select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat, InspectionsUI.Inspections_Declaration, item.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedName, item.Context), item); var references = from item in results.SelectMany(d => d.References) where item.HasTypeHint() select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat, InspectionsUI.Inspections_Usage, item.Declaration.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedModuleName, item.Context), item.Declaration); return(declarations.Union(references)); }
protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { var results = UserDeclarations.ToList(); var declarations = from item in results where item.HasTypeHint select new DeclarationInspectionResult(this, string.Format(InspectionResults.ObsoleteTypeHintInspection, InspectionsUI.Inspections_Declaration, item.DeclarationType.ToString().ToLower(), item.IdentifierName), item); var references = from item in results.SelectMany(d => d.References) where item.HasTypeHint() select new IdentifierReferenceInspectionResult(this, string.Format(InspectionResults.ObsoleteTypeHintInspection, InspectionsUI.Inspections_Usage, item.Declaration.DeclarationType.ToString().ToLower(), item.IdentifierName), State, item); return(declarations.Union <IInspectionResult>(references)); }
public override IEnumerable <InspectionResultBase> GetInspectionResults() { var declarations = UserDeclarations.ToList(); IEnumerable <Declaration> interfaceMembers = null; interfaceMembers = declarations.FindInterfaceMembers() .Concat(declarations.FindInterfaceImplementationMembers()) .ToList(); var formEventHandlerScopes = declarations.FindFormEventHandlers() .Select(handler => handler.Scope); var eventScopes = declarations.Where(item => !item.IsBuiltIn && item.DeclarationType == DeclarationType.Event) .Select(e => e.Scope); var declareScopes = declarations.Where(item => item.DeclarationType == DeclarationType.LibraryFunction || item.DeclarationType == DeclarationType.LibraryProcedure) .Select(e => e.Scope); var ignoredScopes = formEventHandlerScopes.Concat(eventScopes).Concat(declareScopes); var issues = declarations.Where(declaration => !declaration.IsArray() && !ignoredScopes.Contains(declaration.ParentScope) && declaration.DeclarationType == DeclarationType.Parameter && !interfaceMembers.Select(m => m.Scope).Contains(declaration.ParentScope) && ((VBAParser.ArgContext)declaration.Context).BYVAL() == null && !IsUsedAsByRefParam(declarations, declaration) && !declaration.References.Any(reference => reference.IsAssignment)) .Select(issue => new ParameterCanBeByValInspectionResult(this, issue, ((dynamic)issue.Context).identifier(), issue.QualifiedName)); return(issues); }