@@ -26,6 +26,11 @@ public ProcedureCanBeWrittenAsFunctionInspection(RubberduckParserState state)
26
26
27
27
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
28
28
{
29
+ if ( ! Listener . Contexts . Any ( ) )
30
+ {
31
+ return Enumerable . Empty < IInspectionResult > ( ) ;
32
+ }
33
+
29
34
var userDeclarations = UserDeclarations . ToList ( ) ;
30
35
var builtinHandlers = State . DeclarationFinder . FindEventHandlers ( ) . ToList ( ) ;
31
36
@@ -38,17 +43,31 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
38
43
39
44
return Listener . Contexts
40
45
. 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 )
47
52
. All ( withEvents => userDeclarations . FindEventProcedures ( withEvents ) == null ) &&
48
53
! builtinHandlers . Contains ( decl ) )
49
54
. Select ( result => new DeclarationInspectionResult ( this ,
50
55
string . Format ( InspectionResults . ProcedureCanBeWrittenAsFunctionInspection , result . IdentifierName ) ,
51
56
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
+ }
52
71
}
53
72
54
73
public class SingleByRefParamArgListListener : VBAParserBaseListener , IInspectionListener
0 commit comments