1
1
using System . Collections . Generic ;
2
2
using System . Diagnostics . CodeAnalysis ;
3
3
using System . Linq ;
4
- using Antlr4 . Runtime ;
5
4
using Rubberduck . Inspections . Abstract ;
6
5
using Rubberduck . Inspections . Results ;
7
- using Rubberduck . Parsing ;
8
6
using Rubberduck . Parsing . Inspections . Abstract ;
9
7
using Rubberduck . Resources . Inspections ;
10
8
using Rubberduck . Parsing . Symbols ;
@@ -18,6 +16,15 @@ public sealed class UnassignedVariableUsageInspection : InspectionBase
18
16
public UnassignedVariableUsageInspection ( RubberduckParserState state )
19
17
: base ( state ) { }
20
18
19
+ //See https://github.com/rubberduck-vba/Rubberduck/issues/2010 for why these are being excluded.
20
+ private static readonly List < string > IgnoredFunctions = new List < string >
21
+ {
22
+ "VBE7.DLL;VBA.Strings.Len" ,
23
+ "VBE7.DLL;VBA.Strings.LenB" ,
24
+ "VBA6.DLL;VBA.Strings.Len" ,
25
+ "VBA6.DLL;VBA.Strings.LenB"
26
+ } ;
27
+
21
28
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
22
29
{
23
30
var declarations = State . DeclarationFinder . UserDeclarations ( DeclarationType . Variable )
@@ -27,42 +34,16 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
27
34
&& ! declaration . IsSelfAssigned
28
35
&& ! declaration . References . Any ( reference => reference . IsAssignment ) ) ;
29
36
30
- //See https://github.com/rubberduck-vba/Rubberduck/issues/2010 for why these are being excluded.
31
- //TODO: These need to be modified to correctly work in VB6.
32
- var lenFunction = BuiltInDeclarations . SingleOrDefault ( s => s . DeclarationType == DeclarationType . Function && s . Scope . Equals ( "VBE7.DLL;VBA.Strings.Len" ) ) ;
33
- var lenbFunction = BuiltInDeclarations . SingleOrDefault ( s => s . DeclarationType == DeclarationType . Function && s . Scope . Equals ( "VBE7.DLL;VBA.Strings.LenB" ) ) ;
37
+ var excludedDeclarations = BuiltInDeclarations . Where ( decl => IgnoredFunctions . Contains ( decl . QualifiedName . ToString ( ) ) ) . ToList ( ) ;
34
38
35
- return declarations . Where ( d => d . References . Any ( ) &&
36
- ! DeclarationReferencesContainsReference ( lenFunction , d ) &&
37
- ! DeclarationReferencesContainsReference ( lenbFunction , d ) )
38
- . SelectMany ( d => d . References )
39
- . Where ( r => ! r . IsIgnoringInspectionResultFor ( AnnotationName ) )
40
- . Select ( r => new IdentifierReferenceInspectionResult ( this ,
41
- string . Format ( InspectionResults . UnassignedVariableUsageInspection , r . IdentifierName ) ,
42
- State ,
43
- r ) ) . ToList ( ) ;
44
- }
45
-
46
- private bool DeclarationReferencesContainsReference ( Declaration parentDeclaration , Declaration target )
47
- {
48
- if ( parentDeclaration == null )
49
- {
50
- return false ;
51
- }
52
-
53
- foreach ( var targetReference in target . References )
54
- {
55
- foreach ( var reference in parentDeclaration . References )
56
- {
57
- var context = ( ParserRuleContext ) reference . Context . Parent ;
58
- if ( context . GetSelection ( ) . Contains ( targetReference . Selection ) )
59
- {
60
- return true ;
61
- }
62
- }
63
- }
64
-
65
- return false ;
39
+ return declarations . Except ( excludedDeclarations )
40
+ . Where ( d => d . References . Any ( ) )
41
+ . SelectMany ( d => d . References )
42
+ . Where ( r => ! r . IsIgnoringInspectionResultFor ( AnnotationName ) )
43
+ . Select ( r => new IdentifierReferenceInspectionResult ( this ,
44
+ string . Format ( InspectionResults . UnassignedVariableUsageInspection , r . IdentifierName ) ,
45
+ State ,
46
+ r ) ) . ToList ( ) ;
66
47
}
67
48
}
68
49
}
0 commit comments