Skip to content

Commit 5c4fe62

Browse files
committed
Address most of the initial comments
1 parent 51edf3f commit 5c4fe62

File tree

5 files changed

+122
-185
lines changed

5 files changed

+122
-185
lines changed

Rubberduck.Inspections/Concrete/UnreachableCaseInspection/UCIParseTreeValueVisitor.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void Visit(VBAParser.LExprContext context)
123123
}
124124

125125
IUCIValue newResult = null;
126-
if (TryGetTheLExprValue(context, out string lexprValue, out string declaredType))
126+
if (TryGetLExprValue(context, out string lexprValue, out string declaredType))
127127
{
128128
newResult = _inspValueFactory.Create(lexprValue, declaredType);
129129
}
@@ -237,32 +237,36 @@ private static IEnumerable<IParseTree> NonWhitespaceChildren(ParserRuleContext p
237237
return ptParent.children.Where(ch => !(ch is VBAParser.WhiteSpaceContext));
238238
}
239239

240-
private bool TryGetTheLExprValue(VBAParser.LExprContext ctxt, out string expressionValue, out string declaredTypeName)
240+
private bool TryGetLExprValue(VBAParser.LExprContext lExprContext, out string expressionValue, out string declaredTypeName)
241241
{
242242
expressionValue = string.Empty;
243243
declaredTypeName = string.Empty;
244-
if (ctxt.TryGetChildContext(out VBAParser.MemberAccessExprContext memberAccess))
244+
var isMemberAccess = lExprContext.TryGetChildContext(out VBAParser.MemberAccessExprContext memberAccess);
245+
var isSimpleName = lExprContext.TryGetChildContext(out VBAParser.SimpleNameExprContext smplName);
246+
247+
if (!(isMemberAccess || isSimpleName))
248+
{
249+
return false;
250+
}
251+
252+
if (isMemberAccess)
245253
{
246254
var member = memberAccess.GetChild<VBAParser.UnrestrictedIdentifierContext>();
247255

248-
if (TryGetIdentifierReferenceForContext(member, out IdentifierReference idRef))
256+
if (TryGetIdentifierReferenceForContext(member, out IdentifierReference idRef)
257+
&& idRef.Declaration.DeclarationType.HasFlag(DeclarationType.EnumerationMember)
258+
&& idRef.Declaration.Context is VBAParser.EnumerationStmt_ConstantContext)
249259
{
250260
var dec = idRef.Declaration;
251-
if (dec.DeclarationType.HasFlag(DeclarationType.EnumerationMember))
252-
{
253-
var theCtxt = dec.Context;
254-
if (theCtxt is VBAParser.EnumerationStmt_ConstantContext)
255-
{
256-
expressionValue = GetConstantDeclarationValueToken(dec);
257-
declaredTypeName = dec.AsTypeIsBaseType ? dec.AsTypeName : dec.AsTypeDeclaration.AsTypeName;
258-
return true;
259-
}
260-
}
261+
var theCtxt = dec.Context;
262+
expressionValue = GetConstantDeclarationValueToken(dec);
263+
declaredTypeName = dec.AsTypeIsBaseType ? dec.AsTypeName : dec.AsTypeDeclaration.AsTypeName;
264+
return true;
261265
}
262266
return false;
263267
}
264268

265-
if (ctxt.TryGetChildContext(out VBAParser.SimpleNameExprContext smplName))
269+
if (isSimpleName)
266270
{
267271
if (TryGetIdentifierReferenceForContext(smplName, out IdentifierReference rangeClauseIdentifierReference))
268272
{
@@ -279,7 +283,7 @@ private bool TryGetTheLExprValue(VBAParser.LExprContext ctxt, out string express
279283
return false;
280284
}
281285

282-
private bool TryGetIdentifierReferenceForContext<T>(T context, out IdentifierReference idRef) where T : ParserRuleContext
286+
private bool TryGetIdentifierReferenceForContext(ParserRuleContext context, out IdentifierReference idRef)
283287
{
284288
idRef = null;
285289
var identifierReferences = (_state.DeclarationFinder.MatchName(context.GetText()).Select(dec => dec.References)).SelectMany(rf => rf);
@@ -327,8 +331,8 @@ private static string GetBaseTypeForDeclaration(Declaration declaration)
327331

328332
private static bool IsBinaryMathContext<T>(T context)
329333
{
330-
return context is VBAParser.MultOpContext
331-
|| context is VBAParser.AddOpContext
334+
return context is VBAParser.MultOpContext //MultOpContext includes both * and /
335+
|| context is VBAParser.AddOpContext //AddOpContet includes both + and -
332336
|| context is VBAParser.PowOpContext
333337
|| context is VBAParser.ModOpContext;
334338
}

0 commit comments

Comments
 (0)