Skip to content

Commit 97b0b54

Browse files
authored
Merge pull request #6048 from MDoerner/FixConstantNotUsedForArrayBounds
Fix constant not used for array bounds
2 parents 4b2f10c + dceed09 commit 97b0b54

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ constantExpression : expression;
561561

562562
variableStmt : (DIM | STATIC | visibility) whiteSpace variableListStmt;
563563
variableListStmt : variableSubStmt (whiteSpace? COMMA whiteSpace? variableSubStmt)*;
564-
variableSubStmt : (WITHEVENTS whiteSpace)? identifier (whiteSpace? LPAREN whiteSpace? (subscripts whiteSpace?)? RPAREN)? (whiteSpace asTypeClause)?;
564+
variableSubStmt : (WITHEVENTS whiteSpace)? identifier (whiteSpace? arrayDim)? (whiteSpace asTypeClause)?;
565565

566566
whileWendStmt :
567567
WHILE whiteSpace expression endOfStatement
@@ -589,10 +589,6 @@ pSetSpecialForm : (expression whiteSpace? DOT whiteSpace?)? PSET (whiteSpace STE
589589
tuple : LPAREN whiteSpace? expression whiteSpace? COMMA whiteSpace? expression whiteSpace? RPAREN;
590590
lineSpecialFormOption : {EqualsStringIgnoringCase(TextOf(TokenAtRelativePosition(1)),"b","bf")}? unrestrictedIdentifier;
591591

592-
subscripts : subscript (whiteSpace? COMMA whiteSpace? subscript)*;
593-
594-
subscript : (expression whiteSpace TO whiteSpace)? expression;
595-
596592
unrestrictedIdentifier : identifier | statementKeyword | markerKeyword;
597593
legalLabelIdentifier : { !IsTokenType(TokenTypeAtRelativePosition(1),DOEVENTS,END,CLOSE,ELSE,LOOP,NEXT,RANDOMIZE,REM,RESUME,RETURN,STOP,WEND)}? identifier | markerKeyword;
598594
//The predicate in the following rule has been introduced to lessen the problem that VBA uses the same characters used as type hints in other syntactical constructs,

Rubberduck.Parsing/VBA/DeclarationResolving/DeclarationSymbolsListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ public override void EnterVariableSubStmt(VBAParser.VariableSubStmtContext conte
688688
: SymbolList.TypeHintToTypeName[typeHint];
689689
var withEvents = context.WITHEVENTS() != null;
690690
var isAutoObject = asTypeClause != null && asTypeClause.NEW() != null;
691-
bool isArray = context.LPAREN() != null;
691+
bool isArray = context.arrayDim() != null;
692692
AddDeclaration(
693693
CreateDeclaration(
694694
name,

Rubberduck.Refactorings/Common/CodeBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Decl
390390

391391
if (prototype.IsArray)
392392
{
393-
var identifierExpression = prototype.Context.TryGetChildContext<VBAParser.SubscriptsContext>(out var ctxt)
394-
? $"{udtMemberIdentifier}({ctxt.GetText()})"
395-
: $"{udtMemberIdentifier}()";
393+
var identifierExpression = prototype.Context.TryGetChildContext<VBAParser.ArrayDimContext>(out var ctxt)
394+
? $"{udtMemberIdentifier}{ctxt.GetText()}"
395+
: $"{udtMemberIdentifier}()"; // This should never happen.
396396

397397
return $"{identifierExpression} {Tokens.As} {asTypeName}";
398398
}

Rubberduck.Refactorings/MoveCloserToUsage/MoveCloserToUsageRefactoringAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected override void Refactor(MoveCloserToUsageModel model, IRewriteSession r
3434

3535
private void InsertNewDeclaration(VariableDeclaration target, IRewriteSession rewriteSession, string DeclarationStatement)
3636
{
37-
var subscripts = target.Context.GetDescendent<VBAParser.SubscriptsContext>()?.GetText() ?? string.Empty;
37+
var subscripts = target.Context.GetDescendent<VBAParser.BoundsListContext>()?.GetText() ?? string.Empty;
3838
var identifier = target.IsArray ? $"{target.IdentifierName}({subscripts})" : target.IdentifierName;
3939

4040
var newVariable = target.AsTypeContext is null

RubberduckTests/Inspections/ConstantNotUsedInspectionTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ Public Sub Goo(ByVal arg1 As Integer)
123123
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
124124
}
125125

126+
[Test]
127+
[Category("Inspections")]
128+
// See issue #6042 at https://github.com/rubberduck-vba/Rubberduck/issues/6042
129+
public void ConstantNotUsed_DoesNotReturnResult_UsedOnlyInArrayUpperBound()
130+
{
131+
var inputCode =
132+
$@"
133+
Sub Test1()
134+
Const MY_CONST As Byte = 5
135+
Dim tmpArr(1 To MY_CONST) As Variant
136+
Dim tmpArr(1 To MY_CONST)
137+
End Sub
138+
";
139+
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
140+
}
141+
142+
[Test]
143+
[Category("Inspections")]
144+
public void ConstantNotUsed_DoesNotReturnResult_UsedOnlyInArrayLowerBound()
145+
{
146+
var inputCode =
147+
$@"
148+
Sub Test1()
149+
Const MY_CONST As Byte = 5
150+
Dim tmpArr(MY_CONST To 1) As Variant
151+
Dim tmpArr(MY_CONST To 1)
152+
End Sub
153+
";
154+
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
155+
}
156+
126157
[Test]
127158
[Category("Inspections")]
128159
//See issue #4994 at https://github.com/rubberduck-vba/Rubberduck/issues/4994

0 commit comments

Comments
 (0)