private IEnumerable <IdentifierReference> InterestingReferences() { var result = new List <IdentifierReference>(); foreach (var moduleReferences in State.DeclarationFinder.IdentifierReferences()) { var module = State.DeclarationFinder.ModuleDeclaration(moduleReferences.Key); if (module == null || !module.IsUserDefined || IsIgnoringInspectionResultFor(module, AnnotationName)) { // module isn't user code (?), or this inspection is ignored at module-level continue; } foreach (var reference in moduleReferences.Value) { if (!IsIgnoringInspectionResultFor(reference, AnnotationName) && VariableRequiresSetAssignmentEvaluator.NeedsSetKeywordAdded(reference, State)) { result.Add(reference); } } } return(result); }
private void InsertLocalVariableDeclarationAndAssignment(IModuleRewriter rewriter, Declaration target, string localIdentifier) { var localVariableDeclaration = $"{Environment.NewLine}{Tokens.Dim} {localIdentifier} {Tokens.As} {target.AsTypeName}{Environment.NewLine}"; var requiresAssignmentUsingSet = target.References.Any(refItem => VariableRequiresSetAssignmentEvaluator.RequiresSetAssignment(refItem, _parserState)); var localVariableAssignment = requiresAssignmentUsingSet ? $"Set {localIdentifier} = {target.IdentifierName}" : $"{localIdentifier} = {target.IdentifierName}"; rewriter.InsertBefore(((ParserRuleContext)target.Context.Parent).Stop.TokenIndex + 1, localVariableDeclaration + localVariableAssignment); }
private void InsertLocalVariableDeclarationAndAssignment(IModuleRewriter rewriter, Declaration target, string localIdentifier) { var localVariableDeclaration = $"{Tokens.Dim} {localIdentifier} {Tokens.As} {target.AsTypeName}"; var requiresAssignmentUsingSet = target.References.Any(refItem => VariableRequiresSetAssignmentEvaluator.RequiresSetAssignment(refItem, _declarationFinderProvider)); var localVariableAssignment = $"{(requiresAssignmentUsingSet ? $"{Tokens.Set} " : string.Empty)}{localIdentifier} = {target.IdentifierName}"; var endOfStmtCtxt = ((ParserRuleContext)target.Context.Parent.Parent).GetChild <VBAParser.EndOfStatementContext>(); var eosContent = endOfStmtCtxt.GetText(); var idxLastNewLine = eosContent.LastIndexOf(Environment.NewLine, StringComparison.InvariantCultureIgnoreCase); var endOfStmtCtxtComment = eosContent.Substring(0, idxLastNewLine); var endOfStmtCtxtEndFormat = eosContent.Substring(idxLastNewLine); var insertCtxt = ((ParserRuleContext)target.Context.Parent.Parent).GetChild <VBAParser.AsTypeClauseContext>() ?? (ParserRuleContext)target.Context.Parent; rewriter.Remove(endOfStmtCtxt); rewriter.InsertAfter(insertCtxt.Stop.TokenIndex, $"{endOfStmtCtxtComment}{endOfStmtCtxtEndFormat}{localVariableDeclaration}" + $"{endOfStmtCtxtEndFormat}{localVariableAssignment}{endOfStmtCtxtEndFormat}"); }
protected override IEnumerable <IInspectionResult> DoGetInspectionResults() { var allInterestingDeclarations = VariableRequiresSetAssignmentEvaluator.GetDeclarationsPotentiallyRequiringSetAssignment(State.AllUserDeclarations); var candidateReferencesRequiringSetAssignment = allInterestingDeclarations .SelectMany(dec => dec.References) .Where(reference => !IsIgnoringInspectionResultFor(reference, AnnotationName)) .Where(reference => reference.IsAssignment); var referencesRequiringSetAssignment = candidateReferencesRequiringSetAssignment .Where(reference => VariableRequiresSetAssignmentEvaluator.RequiresSetAssignment(reference, State)); var objectVariableNotSetReferences = referencesRequiringSetAssignment.Where(FlagIfObjectVariableNotSet); return(objectVariableNotSetReferences .Select(reference => new IdentifierReferenceInspectionResult(this, string.Format(InspectionsUI.ObjectVariableNotSetInspectionResultFormat, reference.Declaration.IdentifierName), State, reference))); }