Skip to content

Commit 05188be

Browse files
fourlscirras
authored andcommitted
Fix IndexLastListElement issues in assignment expressions
1 parent 8fa8ce4 commit 05188be

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
### Fixed
2121

2222
- Parsing errors where adjacent `>` and `=` tokens were wrongly interpreted as the `>=` operator.
23+
- False-positives within assignment statements in `IndexLastListElement`.
2324

2425
## [1.13.0] - 2025-02-05
2526

delphi-checks/src/main/java/au/com/integradev/delphi/checks/IndexLastListElementCheck.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Optional;
2323
import org.sonar.check.Rule;
2424
import org.sonar.plugins.communitydelphi.api.ast.ArrayAccessorNode;
25+
import org.sonar.plugins.communitydelphi.api.ast.AssignmentStatementNode;
2526
import org.sonar.plugins.communitydelphi.api.ast.BinaryExpressionNode;
2627
import org.sonar.plugins.communitydelphi.api.ast.CommonDelphiNode;
2728
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
@@ -47,6 +48,13 @@ public DelphiCheckContext visit(ArrayAccessorNode arrayTypeNode, DelphiCheckCont
4748
return super.visit(arrayTypeNode, context);
4849
}
4950

51+
@Override
52+
public DelphiCheckContext visit(
53+
AssignmentStatementNode assignmentStatementNode, DelphiCheckContext context) {
54+
// Ignore LHS of assignment statements - TList.Last is a function, so can't be put here
55+
return super.visit(assignmentStatementNode.getValue(), context);
56+
}
57+
5058
private void doVisit(ArrayAccessorNode arrayTypeNode, DelphiCheckContext context) {
5159
List<ExpressionNode> expressions = arrayTypeNode.getExpressions();
5260
if (expressions.size() != 1) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,22 @@ void testMatchingExpressionShouldAddIssue(String use) {
198198
.appendImpl("end;"))
199199
.verifyIssues();
200200
}
201+
202+
@Test
203+
void testAssignmentShouldNotAddIssue() {
204+
CheckVerifier.newVerifier()
205+
.withCheck(new IndexLastListElementCheck())
206+
.withStandardLibraryUnit(systemGenericsCollections())
207+
.onFile(
208+
new DelphiTestUnitBuilder()
209+
.appendImpl("uses")
210+
.appendImpl(" System.Generics.Collections;")
211+
.appendImpl("procedure Test;")
212+
.appendImpl("var")
213+
.appendImpl(" List: TList<Integer>;")
214+
.appendImpl("begin")
215+
.appendImpl(" List[List.Count - 1] := 5;")
216+
.appendImpl("end;"))
217+
.verifyNoIssues();
218+
}
201219
}

0 commit comments

Comments
 (0)