1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
4
+ using Antlr4 . Runtime ;
3
5
using Rubberduck . Inspections . Abstract ;
4
6
using Rubberduck . Inspections . Results ;
5
7
using Rubberduck . Parsing . Inspections . Abstract ;
6
8
using Rubberduck . Resources . Inspections ;
7
9
using Rubberduck . Parsing . Symbols ;
8
10
using Rubberduck . Parsing . VBA ;
9
11
using Rubberduck . Inspections . Inspections . Extensions ;
12
+ using Rubberduck . Parsing ;
13
+ using Rubberduck . Parsing . Grammar ;
10
14
using Rubberduck . Parsing . VBA . DeclarationCaching ;
15
+ using Rubberduck . VBEditor ;
11
16
12
17
namespace Rubberduck . Inspections . Concrete
13
18
{
@@ -45,10 +50,9 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
45
50
{
46
51
var finder = State . DeclarationFinder ;
47
52
48
- var failedLetResolutionAssignments = FailedLetResolutionAssignments ( finder ) ;
49
- var interestingOtherReferences = InterestingReferences ( ) ;
53
+ var failedLetResolutionResults = FailedLetResolutionResults ( finder ) ;
50
54
51
- return failedLetResolutionAssignments . Concat ( interestingOtherReferences )
55
+ return failedLetResolutionResults
52
56
. Select ( reference =>
53
57
new IdentifierReferenceInspectionResult (
54
58
this ,
@@ -57,7 +61,7 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
57
61
reference ) ) ;
58
62
}
59
63
60
- private IEnumerable < IdentifierReference > FailedLetResolutionAssignments ( DeclarationFinder finder )
64
+ private IEnumerable < IdentifierReference > FailedLetResolutionResults ( DeclarationFinder finder )
61
65
{
62
66
var results = new List < IdentifierReference > ( ) ;
63
67
foreach ( var moduleDeclaration in finder . UserDeclarations ( DeclarationType . Module ) )
@@ -67,33 +71,55 @@ private IEnumerable<IdentifierReference> FailedLetResolutionAssignments(Declarat
67
71
continue ;
68
72
}
69
73
70
- var failedLetCoercionAssignmentsInModule = finder
71
- . FailedLetCoercions ( moduleDeclaration . QualifiedModuleName )
72
- . Where ( reference => reference . IsAssignment ) ;
73
-
74
+ var module = moduleDeclaration . QualifiedModuleName ;
75
+ var failedLetCoercionAssignmentsInModule = FailedLetResolutionAssignments ( module , finder ) ;
76
+ var possiblyObjectLhsLetAssignmentsWithFailedLetResolutionOnRhs = PossiblyObjectLhsLetAssignmentsWithNonValueOnRhs ( module , finder ) ;
74
77
results . AddRange ( failedLetCoercionAssignmentsInModule ) ;
78
+ results . AddRange ( possiblyObjectLhsLetAssignmentsWithFailedLetResolutionOnRhs ) ;
75
79
}
76
80
77
81
return results . Where ( reference => ! reference . IsIgnoringInspectionResultFor ( AnnotationName ) ) ;
78
82
}
79
83
80
- private IEnumerable < IdentifierReference > InterestingReferences ( )
84
+ private static IEnumerable < IdentifierReference > FailedLetResolutionAssignments ( QualifiedModuleName module , DeclarationFinder finder )
81
85
{
82
- var result = new List < IdentifierReference > ( ) ;
83
- foreach ( var moduleReferences in State . DeclarationFinder . IdentifierReferences ( ) )
84
- {
85
- var module = State . DeclarationFinder . ModuleDeclaration ( moduleReferences . Key ) ;
86
- if ( module == null || ! module . IsUserDefined || module . IsIgnoringInspectionResultFor ( AnnotationName ) )
87
- {
88
- // module isn't user code (?), or this inspection is ignored at module-level
89
- continue ;
90
- }
86
+ return finder . FailedLetCoercions ( module )
87
+ . Where ( reference => reference . IsAssignment ) ;
88
+ }
91
89
92
- result . AddRange ( moduleReferences . Value . Where ( reference => ! reference . IsSetAssignment
93
- && VariableRequiresSetAssignmentEvaluator . RequiresSetAssignment ( reference , State ) ) ) ;
94
- }
90
+ private static IEnumerable < IdentifierReference > PossiblyObjectLhsLetAssignmentsWithNonValueOnRhs ( QualifiedModuleName module , DeclarationFinder finder )
91
+ {
92
+ return PossiblyObjectLhsLetAssignments ( module , finder )
93
+ . Where ( tpl => finder . FailedLetCoercions ( module )
94
+ . Any ( reference => reference . Selection . Equals ( tpl . rhs . GetSelection ( ) ) )
95
+ || Tokens . Nothing . Equals ( tpl . rhs . GetText ( ) , StringComparison . InvariantCultureIgnoreCase ) )
96
+ . Select ( tpl => tpl . assignment ) ;
97
+ }
98
+
99
+ private static IEnumerable < ( IdentifierReference assignment , ParserRuleContext rhs ) > PossiblyObjectLhsLetAssignments ( QualifiedModuleName module , DeclarationFinder finder )
100
+ {
101
+ return PossiblyObjectNonSetAssignments ( module , finder )
102
+ . Select ( reference => ( reference , RhsOfLetAssignment ( reference ) ) )
103
+ . Where ( tpl => tpl . Item2 != null ) ;
104
+ }
105
+
106
+ private static ParserRuleContext RhsOfLetAssignment ( IdentifierReference letAssignment )
107
+ {
108
+ var letStatement = letAssignment . Context . Parent as VBAParser . LetStmtContext ;
109
+ return letStatement ? . expression ( ) ;
110
+ }
111
+
112
+ private static IEnumerable < IdentifierReference > PossiblyObjectNonSetAssignments ( QualifiedModuleName module , DeclarationFinder finder )
113
+ {
114
+ var assignments = finder . IdentifierReferences ( module )
115
+ . Where ( reference => reference . IsAssignment
116
+ && ! reference . IsSetAssignment
117
+ && ( reference . IsNonIndexedDefaultMemberAccess
118
+ || Tokens . Variant . Equals ( reference . Declaration . AsTypeName , StringComparison . InvariantCultureIgnoreCase ) ) ) ;
119
+ var unboundAssignments = finder . UnboundDefaultMemberAccesses ( module )
120
+ . Where ( reference => reference . IsAssignment ) ;
95
121
96
- return result . Where ( reference => ! reference . IsIgnoringInspectionResultFor ( AnnotationName ) ) ;
122
+ return assignments . Concat ( unboundAssignments ) ;
97
123
}
98
124
}
99
125
}
0 commit comments