It is possible to find while/until/repeat loops that could be safely replaced to foreach.
If the loop condition variable is increasing and only using to access a map element, we should suggest replacing the entire loop with foreach:
let m: map<Int, Int>;
let mSize: Int;
fun test() {
let i: Int = 0;
while (i < self.mSize) {
i = i + 1;
dump(self.m.get(i));
}
}
Could be replaced with:
let m: map<Int, Int>;
let mSize: Int;
fun test() {
let i: Int = 0;
foreach (_k, v in self.m) {
dump(v);
}
}
Things to consider:
The condition variable should be limited by the size of the map. However, there is no .size or .length operation in Tact maps. Therefore, we cannot be 100% sure if the user intended to iterate the whole map there.
We should have types in AST nodes first, which requires the updated type checker in the compiler (#70)
It is possible to find
while
/until
/repeat
loops that could be safely replaced toforeach
.If the loop condition variable is increasing and only using to access a map element, we should suggest replacing the entire loop with
foreach
:Could be replaced with:
Things to consider:
.size
or.length
operation in Tact maps. Therefore, we cannot be 100% sure if the user intended to iterate the whole map there.