Skip to content

Commit 26eb6c9

Browse files
committed
account for enums, clean up dead code
1 parent 97c224d commit 26eb6c9

File tree

4 files changed

+12
-88
lines changed

4 files changed

+12
-88
lines changed

Rubberduck.Inspections/VariableRequiresSetAssignmentEvaluator.cs

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu
5555
return false;
5656
}
5757

58-
var isObjectVariable = declaration.IsObject();
58+
var isObjectVariable = declaration.IsObject;
5959
var isVariant = declaration.IsUndeclared || declaration.AsTypeName == Tokens.Variant;
6060
if (!isObjectVariable && !isVariant)
6161
{
@@ -180,76 +180,5 @@ private static bool IsVariableOrParameter(Declaration item)
180180
return item.DeclarationType == DeclarationType.Variable
181181
|| item.DeclarationType == DeclarationType.Parameter;
182182
}
183-
184-
private static bool ObjectOrVariantRequiresSetAssignment(IdentifierReference objectOrVariantRef, IEnumerable<Declaration> variantAndObjectDeclarations)
185-
{
186-
//Not an assignment...nothing to evaluate
187-
if (!objectOrVariantRef.IsAssignment)
188-
{
189-
return false;
190-
}
191-
192-
if (objectOrVariantRef.Declaration.AsTypeName != Tokens.Variant)
193-
{
194-
return true;
195-
}
196-
197-
//Variants can be assigned with or without 'Set' depending...
198-
var letStmtContext = objectOrVariantRef.Context.GetAncestor<VBAParser.LetStmtContext>();
199-
200-
//A potential error is only possible for let statements: rset, lset and other type specific assignments are always let assignments;
201-
//assignemts in for each loop statements are do not require the set keyword.
202-
if(letStmtContext == null)
203-
{
204-
return false;
205-
}
206-
207-
//You can only new up objects.
208-
if (RHSUsesNew(letStmtContext))
209-
{
210-
return true;
211-
}
212-
213-
if (RHSIsLiteral(letStmtContext))
214-
{
215-
if(RHSIsObjectLiteral(letStmtContext))
216-
{
217-
return true;
218-
}
219-
//All literals but the object literal potentially do not need a set assignment.
220-
//We cannot get more information from the RHS and do not want false positives.
221-
return false;
222-
}
223-
224-
//If the RHS is the identifierName of one of the 'interesting' declarations, we need to use 'Set'
225-
//unless the 'interesting' declaration is also a Variant
226-
var rhsIdentifier = GetRHSIdentifierExpressionText(letStmtContext);
227-
return variantAndObjectDeclarations.Any(dec => dec.IdentifierName == rhsIdentifier
228-
&& dec.AsTypeName != Tokens.Variant
229-
&& dec.Attributes.HasDefaultMemberAttribute(dec.IdentifierName, out _));
230-
}
231-
232-
private static string GetRHSIdentifierExpressionText(VBAParser.LetStmtContext letStmtContext)
233-
{
234-
var expression = letStmtContext.expression();
235-
return expression is VBAParser.LExprContext ? expression.GetText() : string.Empty;
236-
}
237-
238-
private static bool RHSUsesNew(VBAParser.LetStmtContext letStmtContext)
239-
{
240-
var expression = letStmtContext.expression();
241-
return (expression is VBAParser.NewExprContext);
242-
}
243-
244-
private static bool RHSIsLiteral(VBAParser.LetStmtContext letStmtContext)
245-
{
246-
return letStmtContext.expression() is VBAParser.LiteralExprContext;
247-
}
248-
249-
private static bool RHSIsObjectLiteral(VBAParser.LetStmtContext letStmtContext)
250-
{
251-
var rhsAsLiteralExpr = letStmtContext.expression() as VBAParser.LiteralExprContext;
252-
return rhsAsLiteralExpr?.literalExpression()?.literalIdentifier()?.objectLiteralIdentifier() != null;
253-
}
254183
}
255184
}

Rubberduck.Parsing/Symbols/Declaration.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,13 @@ private static string CorrectlyFormatedDescription(string literalDescription)
339339
/// </summary>
340340
public bool IsEnumeratorMember => _attributes.Any(a => a.Name.EndsWith("VB_UserMemId") && a.Values.Contains("-4"));
341341

342-
public virtual bool IsObject()
343-
{
344-
return AsTypeName == Tokens.Object
345-
|| (AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.ClassModule) ?? false)
346-
|| (!AsTypeIsBaseType && !IsArray && !DeclarationType.HasFlag(DeclarationType.UserDefinedType));
347-
}
342+
public virtual bool IsObject =>
343+
AsTypeName == Tokens.Object
344+
|| (AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.ClassModule) ?? false)
345+
&& !AsTypeIsBaseType
346+
&& !IsArray
347+
&& !DeclarationType.HasFlag(DeclarationType.UserDefinedType)
348+
&& !DeclarationType.HasFlag(DeclarationType.Enumeration);
348349

349350
public void AddReference(
350351
QualifiedModuleName module,

Rubberduck.Parsing/Symbols/PropertyLetDeclaration.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ public void AddParameter(ParameterDeclaration parameter)
7777
/// </summary>
7878
public bool IsDefaultMember => Attributes.Any(a => a.Name == $"{IdentifierName}.VB_UserMemId" && a.Values.Single() == "0");
7979

80-
public override bool IsObject()
81-
{
82-
return base.IsObject()
83-
|| (Parameters.OrderBy(p => p.Selection).LastOrDefault()?.IsObject() ?? false);
84-
}
80+
public override bool IsObject =>
81+
base.IsObject || (Parameters.OrderBy(p => p.Selection).LastOrDefault()?.IsObject ?? false);
8582
}
8683
}

Rubberduck.Parsing/Symbols/PropertySetDeclaration.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ public void AddParameter(ParameterDeclaration parameter)
7676
/// </summary>
7777
public bool IsDefaultMember => Attributes.Any(a => a.Name == $"{IdentifierName}.VB_UserMemId" && a.Values.Single() == "0");
7878

79-
public override bool IsObject()
80-
{
81-
return base.IsObject()
82-
|| (Parameters.OrderBy(p => p.Selection).LastOrDefault()?.IsObject() ?? false);
83-
}
79+
public override bool IsObject =>
80+
base.IsObject || (Parameters.OrderBy(p => p.Selection).LastOrDefault()?.IsObject ?? false);
8481
}
8582
}

0 commit comments

Comments
 (0)