@@ -23,53 +23,61 @@ public ParameterCanBeByValInspection(RubberduckParserState state)
23
23
24
24
public override IEnumerable < InspectionResultBase > GetInspectionResults ( )
25
25
{
26
- var declarations = UserDeclarations . ToList ( ) ;
26
+ var declarations = UserDeclarations . ToArray ( ) ;
27
27
var issues = new List < ParameterCanBeByValInspectionResult > ( ) ;
28
28
29
- var interfaceDeclarationMembers = declarations . FindInterfaceMembers ( ) . ToList ( ) ;
30
- var interfaceScopes = declarations . FindInterfaceImplementationMembers ( ) . Concat ( interfaceDeclarationMembers ) . Select ( s => s . Scope ) ;
29
+ var interfaceDeclarationMembers = declarations . FindInterfaceMembers ( ) . ToArray ( ) ;
30
+ var interfaceScopes = declarations . FindInterfaceImplementationMembers ( ) . Concat ( interfaceDeclarationMembers ) . Select ( s => s . Scope ) . ToArray ( ) ;
31
31
32
32
issues . AddRange ( GetResults ( declarations , interfaceDeclarationMembers ) ) ;
33
33
34
- var eventMembers = declarations . Where ( item => ! item . IsBuiltIn && item . DeclarationType == DeclarationType . Event ) . ToList ( ) ;
35
- var formEventHandlerScopes = State . FindFormEventHandlers ( ) . Select ( handler => handler . Scope ) ;
36
- var eventHandlerScopes = State . DeclarationFinder . FindEventHandlers ( ) . Concat ( declarations . FindUserEventHandlers ( ) ) . Select ( e => e . Scope ) ;
34
+ var eventMembers = declarations . Where ( item => ! item . IsBuiltIn && item . DeclarationType == DeclarationType . Event ) . ToArray ( ) ;
35
+ var formEventHandlerScopes = State . FindFormEventHandlers ( ) . Select ( handler => handler . Scope ) . ToArray ( ) ;
36
+ var eventHandlerScopes = State . DeclarationFinder . FindEventHandlers ( ) . Concat ( declarations . FindUserEventHandlers ( ) ) . Select ( e => e . Scope ) . ToArray ( ) ;
37
37
var eventScopes = eventMembers . Select ( s => s . Scope )
38
38
. Concat ( formEventHandlerScopes )
39
- . Concat ( eventHandlerScopes ) ;
39
+ . Concat ( eventHandlerScopes )
40
+ . ToArray ( ) ;
40
41
41
42
issues . AddRange ( GetResults ( declarations , eventMembers ) ) ;
42
43
43
44
var declareScopes = declarations . Where ( item =>
44
45
item . DeclarationType == DeclarationType . LibraryFunction
45
46
|| item . DeclarationType == DeclarationType . LibraryProcedure )
46
- . Select ( e => e . Scope ) ;
47
+ . Select ( e => e . Scope )
48
+ . ToArray ( ) ;
47
49
48
- issues . AddRange ( declarations . Where ( declaration =>
50
+ issues . AddRange ( declarations . OfType < ParameterDeclaration > ( )
51
+ . Where ( declaration => IsIssue ( declaration , declarations , declareScopes , eventScopes , interfaceScopes ) )
52
+ . Select ( issue => new ParameterCanBeByValInspectionResult ( this , State , issue , issue . Context , issue . QualifiedName ) ) ) ;
53
+
54
+ return issues ;
55
+ }
56
+
57
+ private bool IsIssue ( ParameterDeclaration declaration , Declaration [ ] userDeclarations , string [ ] declareScopes , string [ ] eventScopes , string [ ] interfaceScopes )
58
+ {
59
+ var isIssue =
49
60
! declaration . IsArray
61
+ && ! declaration . IsParamArray
62
+ && ( declaration . IsByRef || declaration . IsImplicitByRef )
50
63
&& ( declaration . AsTypeDeclaration == null || declaration . AsTypeDeclaration . DeclarationType != DeclarationType . UserDefinedType )
51
64
&& ! declareScopes . Contains ( declaration . ParentScope )
52
65
&& ! eventScopes . Contains ( declaration . ParentScope )
53
66
&& ! interfaceScopes . Contains ( declaration . ParentScope )
54
- && declaration . DeclarationType == DeclarationType . Parameter
55
- && ( ( VBAParser . ArgContext ) declaration . Context ) . BYVAL ( ) == null
56
- && ! IsUsedAsByRefParam ( declarations , declaration )
57
- && ! declaration . References . Any ( reference => reference . IsAssignment ) )
58
- . Select ( issue => new ParameterCanBeByValInspectionResult ( this , State , issue , issue . Context , issue . QualifiedName ) ) ) ;
59
-
60
- return issues ;
67
+ && ! IsUsedAsByRefParam ( userDeclarations , declaration )
68
+ && ( ! declaration . References . Any ( ) || ! declaration . References . Any ( reference => reference . IsAssignment ) ) ;
69
+ return isIssue ;
61
70
}
62
71
63
- private IEnumerable < ParameterCanBeByValInspectionResult > GetResults ( List < Declaration > declarations , List < Declaration > declarationMembers )
72
+ private IEnumerable < ParameterCanBeByValInspectionResult > GetResults ( Declaration [ ] declarations , Declaration [ ] declarationMembers )
64
73
{
65
74
foreach ( var declaration in declarationMembers )
66
75
{
67
- var declarationParameters =
68
- declarations . Where ( d => d . DeclarationType == DeclarationType . Parameter &&
69
- Equals ( d . ParentDeclaration , declaration ) )
70
- . OrderBy ( o => o . Selection . StartLine )
71
- . ThenBy ( t => t . Selection . StartColumn )
72
- . ToList ( ) ;
76
+ var declarationParameters = declarations . OfType < ParameterDeclaration > ( )
77
+ . Where ( d => Equals ( d . ParentDeclaration , declaration ) )
78
+ . OrderBy ( o => o . Selection . StartLine )
79
+ . ThenBy ( t => t . Selection . StartColumn )
80
+ . ToList ( ) ;
73
81
74
82
if ( ! declarationParameters . Any ( ) ) { continue ; }
75
83
var parametersAreByRef = declarationParameters . Select ( s => true ) . ToList ( ) ;
@@ -80,12 +88,11 @@ private IEnumerable<ParameterCanBeByValInspectionResult> GetResults(List<Declara
80
88
81
89
foreach ( var member in members )
82
90
{
83
- var parameters =
84
- declarations . Where ( d => d . DeclarationType == DeclarationType . Parameter &&
85
- Equals ( d . ParentDeclaration , member ) )
86
- . OrderBy ( o => o . Selection . StartLine )
87
- . ThenBy ( t => t . Selection . StartColumn )
88
- . ToList ( ) ;
91
+ var parameters = declarations . OfType < ParameterDeclaration > ( )
92
+ . Where ( d => Equals ( d . ParentDeclaration , member ) )
93
+ . OrderBy ( o => o . Selection . StartLine )
94
+ . ThenBy ( t => t . Selection . StartColumn )
95
+ . ToList ( ) ;
89
96
90
97
for ( var i = 0 ; i < parameters . Count ; i ++ )
91
98
{
0 commit comments