Skip to content

Commit a7ccb69

Browse files
committed
Make UnreachableCaseInspection able to run by module
1 parent c4e0fd8 commit a7ccb69

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/UnreachableCaseInspection/UnreachableCaseInspection.cs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System;
1515
using Rubberduck.Inspections.Inspections.Extensions;
1616
using Rubberduck.JunkDrawer.Extensions;
17+
using Rubberduck.Parsing.VBA.DeclarationCaching;
1718
using Rubberduck.Parsing.VBA.Parsing;
1819

1920
namespace Rubberduck.Inspections.Concrete.UnreachableCaseInspection
@@ -143,33 +144,60 @@ public UnreachableCaseInspection(IDeclarationFinderProvider declarationFinderPro
143144
public IInspectionListener Listener { get; } =
144145
new UnreachableCaseInspectionListener();
145146

146-
private List<IInspectionResult> _inspectionResults = new List<IInspectionResult>();
147-
private ParseTreeVisitorResults ValueResults { get; } = new ParseTreeVisitorResults();
147+
private ParseTreeVisitorResults ValueResults { get; } = new ParseTreeVisitorResults();
148148

149149
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
150150
{
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)
154168
// ignore filtering here to make the search space smaller
155-
.Where(result => !result.IsIgnoringInspectionResultFor(DeclarationFinderProvider.DeclarationFinder, AnnotationName));
169+
.Where(result => !result.IsIgnoringInspectionResultFor(finder, AnnotationName));
156170

157171
ParseTreeValueVisitor.OnValueResultCreated += ValueResults.OnNewValueResult;
158172

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+
}
163177

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);
165182

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();
173201
}
174202

175203
private IParseTreeValueVisitor _parseTreeValueVisitor;
@@ -186,12 +214,11 @@ public IParseTreeValueVisitor ParseTreeValueVisitor
186214
}
187215
}
188216

189-
private void CreateInspectionResult(QualifiedContext<ParserRuleContext> selectStmt, ParserRuleContext unreachableBlock, string message)
217+
private IInspectionResult CreateInspectionResult(QualifiedContext<ParserRuleContext> selectStmt, ParserRuleContext unreachableBlock, string message)
190218
{
191-
var result = new QualifiedContextInspectionResult(this,
219+
return new QualifiedContextInspectionResult(this,
192220
message,
193221
new QualifiedContext<ParserRuleContext>(selectStmt.ModuleName, unreachableBlock));
194-
_inspectionResults.Add(result);
195222
}
196223

197224
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
212239
return (false, null);
213240
}
214241

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.
215243
var finder = declarationFinderProvider.DeclarationFinder;
216244
var identifierReferences = finder.MatchName(context.GetText())
217245
.SelectMany(declaration => declaration.References)

0 commit comments

Comments
 (0)