Deleting an item when iterating through collection using the operator "For each ... In ... Do" (DeletingCollectionItem)¶
Type | Scope | Severity | Activated by default |
Minutes to fix |
Tags |
---|---|---|---|---|---|
Error |
BSL OS |
Major |
Yes |
5 |
standard error |
Description¶
Don't delete elements of collection when iterating through collection using the operator For each ... In ... Do. Because it change index of next element.
Example:
For each Element In Collection Do
Collection.Delete(Element)
EndDo;
Alternatively, remove elements from the end:
IndexOf = Numbers.UBound();
While IndexOf >= 0 Do
If Numbers[IndexOf] < 10 Then
Numbers.Delete(IndexOf);
EndIf;
IndexOf = IndexOf – 1;
EndDo;
Sources¶
Snippets¶
Diagnostic ignorance in code¶
// BSLLS:DeletingCollectionItem-off
// BSLLS:DeletingCollectionItem-on
Parameter for config¶
"DeletingCollectionItem": false