Skip to content

Commit 1737bd6

Browse files
committed
Disambiguate const array literals from parenthesized primary expressions
1 parent e6d6315 commit 1737bd6

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Non-trivial `inherited` expressions are excluded in `RedundantParentheses`.
1313

14+
### Fixed
15+
16+
- False positives around const array literals in `RedundantParentheses`.
17+
1418
## [1.9.0] - 2024-09-03
1519

1620
### Added

delphi-checks/src/test/java/au/com/integradev/delphi/checks/RedundantParenthesesCheckTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void testParenthesesOnUnaryExpressionShouldNotAddIssue() {
8989
}
9090

9191
@Test
92-
void testParenthesesOnNonTrivialInheritedExpressionShouldAddIssue() {
92+
void testParenthesesOnNonTrivialInheritedExpressionShouldNotAddIssue() {
9393
CheckVerifier.newVerifier()
9494
.withCheck(new RedundantParenthesesCheck())
9595
.onFile(
@@ -101,6 +101,41 @@ void testParenthesesOnNonTrivialInheritedExpressionShouldAddIssue() {
101101
.verifyNoIssues();
102102
}
103103

104+
@Test
105+
void testSingleElementConstArrayLiteralShouldNotAddIssue() {
106+
CheckVerifier.newVerifier()
107+
.withCheck(new RedundantParenthesesCheck())
108+
.onFile(
109+
new DelphiTestUnitBuilder()
110+
.appendImpl("const")
111+
.appendImpl(" CArray: array[0..0] of Integer = (123);"))
112+
.verifyNoIssues();
113+
}
114+
115+
@Test
116+
void testSingleElementVarArrayLiteralShouldNotAddIssue() {
117+
CheckVerifier.newVerifier()
118+
.withCheck(new RedundantParenthesesCheck())
119+
.onFile(
120+
new DelphiTestUnitBuilder()
121+
.appendImpl("var")
122+
.appendImpl(" A: array[0..0] of Integer = (123);"))
123+
.verifyNoIssues();
124+
}
125+
126+
@Test
127+
void testParenthesesOnPrimaryExpressionWithinSingleElementArrayLiteralShouldAddIssue() {
128+
CheckVerifier.newVerifier()
129+
.withCheck(new RedundantParenthesesCheck())
130+
.onFile(
131+
new DelphiTestUnitBuilder()
132+
.appendImpl("var")
133+
.appendImpl(" // Fix@[+2:31 to +2:32] <<>>")
134+
.appendImpl(" // Fix@[+1:35 to +1:36] <<>>")
135+
.appendImpl(" A: array[0..0] of Integer = ((123)); // Noncompliant"))
136+
.verifyIssues();
137+
}
138+
104139
@Test
105140
void testParenthesesOnParenthesizedExpressionShouldAddIssue() {
106141
CheckVerifier.newVerifier()

delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ constSection : (CONST<ConstSectionNodeImpl>^ | RESOURCESTRING<Co
467467
// example: "const {$include versioninfo.inc}"
468468
// Is this really the appropriate solution?
469469
;
470-
constDeclaration : attributeList? nameDeclaration (':' varType)? '=' constExpression portabilityDirective* ';'
470+
constDeclaration : attributeList? nameDeclaration ':' fixedArrayType '=' arrayExpression portabilityDirective* ';'
471+
-> ^(TkConstDeclaration<ConstDeclarationNodeImpl> nameDeclaration arrayExpression fixedArrayType attributeList? portabilityDirective*)
472+
| attributeList? nameDeclaration (':' varType)? '=' constExpression portabilityDirective* ';'
471473
-> ^(TkConstDeclaration<ConstDeclarationNodeImpl> nameDeclaration constExpression varType? attributeList? portabilityDirective*)
472474
;
473475
typeSection : TYPE<TypeSectionNodeImpl>^ typeDeclaration+
@@ -479,9 +481,14 @@ typeDeclaration : attributeList? genericNameDeclaration '=' typeDec
479481
;
480482
varSection : (VAR<VarSectionNodeImpl>^ | THREADVAR<VarSectionNodeImpl>^) varDeclaration varDeclaration*
481483
;
482-
varDeclaration : attributeList? nameDeclarationList ':' varType portabilityDirective* varValueSpec? portabilityDirective* ';'
484+
varDeclaration : attributeList? nameDeclarationList ':' fixedArrayType portabilityDirective* arrayVarValueSpec? portabilityDirective* ';'
485+
-> ^(TkVarDeclaration<VarDeclarationNodeImpl> nameDeclarationList fixedArrayType arrayVarValueSpec? attributeList?)
486+
| attributeList? nameDeclarationList ':' varType portabilityDirective* varValueSpec? portabilityDirective* ';'
483487
-> ^(TkVarDeclaration<VarDeclarationNodeImpl> nameDeclarationList varType varValueSpec? attributeList?)
484488
;
489+
arrayVarValueSpec : ABSOLUTE expression
490+
| '=' arrayExpression
491+
;
485492
varValueSpec : ABSOLUTE expression
486493
| '=' constExpression
487494
;
@@ -531,6 +538,9 @@ parameterType : stringType
531538
| typeReference
532539
| PACKED parameterType^
533540
;
541+
fixedArrayType : ARRAY arrayIndices OF arrayElementType
542+
-> ^(ARRAY<ArrayTypeNodeImpl> OF arrayElementType arrayIndices )
543+
;
534544
arrayType : ARRAY arrayIndices? OF arrayElementType
535545
-> ^(ARRAY<ArrayTypeNodeImpl> OF arrayElementType arrayIndices? )
536546
;

0 commit comments

Comments
 (0)