Skip to content

Commit 8d78101

Browse files
authored
Merge pull request #4692 from BZngr/4655_KeyNotFoundException
4655 key not found exception
2 parents 13ae992 + 5356e21 commit 8d78101

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/ProcedureCanBeWrittenAsFunctionInspection.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public ProcedureCanBeWrittenAsFunctionInspection(RubberduckParserState state)
2626

2727
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
2828
{
29+
if (!Listener.Contexts.Any())
30+
{
31+
return Enumerable.Empty<IInspectionResult>();
32+
}
33+
2934
var userDeclarations = UserDeclarations.ToList();
3035
var builtinHandlers = State.DeclarationFinder.FindEventHandlers().ToList();
3136

@@ -38,17 +43,31 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
3843

3944
return Listener.Contexts
4045
.Where(context => context.Context.Parent is VBAParser.SubStmtContext
41-
&& contextLookup[context.Context.GetChild<VBAParser.ArgContext>()].References
42-
.Any(reference => reference.IsAssignment))
43-
.Select(context => contextLookup[(VBAParser.SubStmtContext)context.Context.Parent])
44-
.Where(decl => !IsIgnoringInspectionResultFor(decl, AnnotationName) &&
45-
!ignored.Contains(decl) &&
46-
userDeclarations.Where(item => item.IsWithEvents)
46+
&& HasArgumentReferencesWithIsAssignmentFlagged(context))
47+
.Select(context => GetSubStmtParentDeclaration(context))
48+
.Where(decl => decl != null &&
49+
!IsIgnoringInspectionResultFor(decl, AnnotationName) &&
50+
!ignored.Contains(decl) &&
51+
userDeclarations.Where(item => item.IsWithEvents)
4752
.All(withEvents => userDeclarations.FindEventProcedures(withEvents) == null) &&
4853
!builtinHandlers.Contains(decl))
4954
.Select(result => new DeclarationInspectionResult(this,
5055
string.Format(InspectionResults.ProcedureCanBeWrittenAsFunctionInspection, result.IdentifierName),
5156
result));
57+
58+
bool HasArgumentReferencesWithIsAssignmentFlagged(QualifiedContext<ParserRuleContext> context)
59+
{
60+
return contextLookup.TryGetValue(context.Context.GetChild<VBAParser.ArgContext>(), out Declaration decl)
61+
? decl.References.Any(rf => rf.IsAssignment)
62+
: false;
63+
}
64+
65+
Declaration GetSubStmtParentDeclaration(QualifiedContext<ParserRuleContext> context)
66+
{
67+
return contextLookup.TryGetValue(context.Context.Parent as VBAParser.SubStmtContext, out Declaration decl)
68+
? decl
69+
: null;
70+
}
5271
}
5372

5473
public class SingleByRefParamArgListListener : VBAParserBaseListener, IInspectionListener

0 commit comments

Comments
 (0)