14
14
using System ;
15
15
using Rubberduck . Inspections . Inspections . Extensions ;
16
16
using Rubberduck . JunkDrawer . Extensions ;
17
+ using Rubberduck . Parsing . VBA . DeclarationCaching ;
17
18
using Rubberduck . Parsing . VBA . Parsing ;
18
19
19
20
namespace Rubberduck . Inspections . Concrete . UnreachableCaseInspection
@@ -143,33 +144,60 @@ public UnreachableCaseInspection(IDeclarationFinderProvider declarationFinderPro
143
144
public IInspectionListener Listener { get ; } =
144
145
new UnreachableCaseInspectionListener ( ) ;
145
146
146
- private List < IInspectionResult > _inspectionResults = new List < IInspectionResult > ( ) ;
147
- private ParseTreeVisitorResults ValueResults { get ; } = new ParseTreeVisitorResults ( ) ;
147
+ private ParseTreeVisitorResults ValueResults { get ; } = new ParseTreeVisitorResults ( ) ;
148
148
149
149
protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
150
150
{
151
- //FIXME Get the declaration finder only once inside the inspection to avoid possible inconsistent state due to a reparse while inspections run.
152
- _inspectionResults = new List < IInspectionResult > ( ) ;
153
- var qualifiedSelectCaseStmts = Listener . Contexts ( )
151
+ var finder = DeclarationFinderProvider . DeclarationFinder ;
152
+
153
+ return finder . UserDeclarations ( DeclarationType . Module )
154
+ . Where ( module => module != null )
155
+ . SelectMany ( module => DoGetInspectionResults ( module . QualifiedModuleName , finder ) )
156
+ . ToList ( ) ;
157
+ }
158
+
159
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module )
160
+ {
161
+ var finder = DeclarationFinderProvider . DeclarationFinder ;
162
+ return DoGetInspectionResults ( module , finder ) ;
163
+ }
164
+
165
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module , DeclarationFinder finder )
166
+ {
167
+ var qualifiedSelectCaseStmts = Listener . Contexts ( module )
154
168
// ignore filtering here to make the search space smaller
155
- . Where ( result => ! result . IsIgnoringInspectionResultFor ( DeclarationFinderProvider . DeclarationFinder , AnnotationName ) ) ;
169
+ . Where ( result => ! result . IsIgnoringInspectionResultFor ( finder , AnnotationName ) ) ;
156
170
157
171
ParseTreeValueVisitor . OnValueResultCreated += ValueResults . OnNewValueResult ;
158
172
159
- foreach ( var qualifiedSelectCaseStmt in qualifiedSelectCaseStmts )
160
- {
161
- qualifiedSelectCaseStmt . Context . Accept ( ParseTreeValueVisitor ) ;
162
- var selectCaseInspector = _unreachableCaseInspectorFactory . Create ( ( VBAParser . SelectCaseStmtContext ) qualifiedSelectCaseStmt . Context , ValueResults , _valueFactory , GetVariableTypeName ) ;
173
+ return qualifiedSelectCaseStmts
174
+ . SelectMany ( ResultsForContext )
175
+ . ToList ( ) ;
176
+ }
163
177
164
- selectCaseInspector . InspectForUnreachableCases ( ) ;
178
+ private IEnumerable < IInspectionResult > ResultsForContext ( QualifiedContext < ParserRuleContext > qualifiedSelectCaseStmt )
179
+ {
180
+ qualifiedSelectCaseStmt . Context . Accept ( ParseTreeValueVisitor ) ;
181
+ var selectCaseInspector = _unreachableCaseInspectorFactory . Create ( ( VBAParser . SelectCaseStmtContext ) qualifiedSelectCaseStmt . Context , ValueResults , _valueFactory , GetVariableTypeName ) ;
165
182
166
- selectCaseInspector . UnreachableCases . ForEach ( uc => CreateInspectionResult ( qualifiedSelectCaseStmt , uc , ResultMessages [ CaseInspectionResult . Unreachable ] ) ) ;
167
- selectCaseInspector . MismatchTypeCases . ForEach ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . MismatchType ] ) ) ;
168
- selectCaseInspector . OverflowCases . ForEach ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . Overflow ] ) ) ;
169
- selectCaseInspector . InherentlyUnreachableCases . ForEach ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . InherentlyUnreachable ] ) ) ;
170
- selectCaseInspector . UnreachableCaseElseCases . ForEach ( ce => CreateInspectionResult ( qualifiedSelectCaseStmt , ce , ResultMessages [ CaseInspectionResult . CaseElse ] ) ) ;
171
- }
172
- return _inspectionResults ;
183
+ selectCaseInspector . InspectForUnreachableCases ( ) ;
184
+
185
+ return selectCaseInspector
186
+ . UnreachableCases
187
+ . Select ( uc => CreateInspectionResult ( qualifiedSelectCaseStmt , uc , ResultMessages [ CaseInspectionResult . Unreachable ] ) )
188
+ . Concat ( selectCaseInspector
189
+ . MismatchTypeCases
190
+ . Select ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . MismatchType ] ) ) )
191
+ . Concat ( selectCaseInspector
192
+ . OverflowCases
193
+ . Select ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . Overflow ] ) ) )
194
+ . Concat ( selectCaseInspector
195
+ . InherentlyUnreachableCases
196
+ . Select ( mm => CreateInspectionResult ( qualifiedSelectCaseStmt , mm , ResultMessages [ CaseInspectionResult . InherentlyUnreachable ] ) ) )
197
+ . Concat ( selectCaseInspector
198
+ . UnreachableCaseElseCases
199
+ . Select ( ce => CreateInspectionResult ( qualifiedSelectCaseStmt , ce , ResultMessages [ CaseInspectionResult . CaseElse ] ) ) )
200
+ . ToList ( ) ;
173
201
}
174
202
175
203
private IParseTreeValueVisitor _parseTreeValueVisitor ;
@@ -186,12 +214,11 @@ public IParseTreeValueVisitor ParseTreeValueVisitor
186
214
}
187
215
}
188
216
189
- private void CreateInspectionResult ( QualifiedContext < ParserRuleContext > selectStmt , ParserRuleContext unreachableBlock , string message )
217
+ private IInspectionResult CreateInspectionResult ( QualifiedContext < ParserRuleContext > selectStmt , ParserRuleContext unreachableBlock , string message )
190
218
{
191
- var result = new QualifiedContextInspectionResult ( this ,
219
+ return new QualifiedContextInspectionResult ( this ,
192
220
message ,
193
221
new QualifiedContext < ParserRuleContext > ( selectStmt . ModuleName , unreachableBlock ) ) ;
194
- _inspectionResults . Add ( result ) ;
195
222
}
196
223
197
224
public static IParseTreeValueVisitor CreateParseTreeValueVisitor ( IParseTreeValueFactory valueFactory , IReadOnlyList < VBAParser . EnumerationStmtContext > allEnums , Func < ParserRuleContext , ( bool success , IdentifierReference idRef ) > func )
@@ -212,6 +239,7 @@ public static (bool success, IdentifierReference idRef) GetIdentifierReferenceFo
212
239
return ( false , null ) ;
213
240
}
214
241
242
+ //FIXME Get the declaration finder only once inside the inspection to avoid the possibility of inconsistent state due to a reparse while inspections run.
215
243
var finder = declarationFinderProvider . DeclarationFinder ;
216
244
var identifierReferences = finder . MatchName ( context . GetText ( ) )
217
245
. SelectMany ( declaration => declaration . References )
0 commit comments