sonar-delphi
sonar-delphi copied to clipboard
Option on ArrayTypeNode and ArrayType to get starting and ending array index and range
Prerequisites
- [X] This improvement has not already been suggested.
- [X] This improvement would be generally useful, not specific to my code or setup.
Engine area
Rules API
Improvement description
As said in my question issue (https://github.com/integrated-application-development/sonar-delphi/issues/257), It would be a great improvement if those two classes (ArrayTypeNode and ArrayType) could directly get the starting index, ending index and range of the array. Now, the way of doing this is: acessing ArrayTypeNode, getting the first child of type ArrayIndicesNode, getting the first child of type SubRangeTypeNode, getting lowExpression and highExpression.
It would be good to have something like:
ArrayTypeNode.getStartingIndex();
ArrayTypeNode.getEndingIndex();
ArrayTypeNode.getRange();
ArrayType.getStartingIndex();
ArrayType.getEndingIndex();
ArrayType.getRange();
Rationale
This option could make it easier to create new rules or enhance existing ones for everyone, since it simplifies the way of getting arrays indexes.
One example of usage would be a rule that verifies if a for iteration is done in the correct range. If there is an iteration in an array defined as:
MyArray: array[2..5] of Integer
And the iteration something like:
for Index := 2 to Length(MyArray) -1
That would not work, as the iteration would not be executed in the wanted range. With this improvement, there could be a simple way of verifying the indexes and validate if the iteration is executed the correct range.