for
loops that use length
member of some storage array in their loop condition and don't modify it SLITHER-W1088Detects for
loops that use length
member of some storage array in their loop condition and don't modify it.
contract C
{
uint[] array;
function f() public
{
for (uint i = 0; i < array.length; i++)
{
// code that does not modify length of `array`
}
}
}
Since the for
loop in f
doesn't modify array.length
, it is more gas efficient to cache it in some local variable and use that variable instead, like in the following example:
contract C
{
uint[] array;
function f() public
{
uint array_length = array.length;
for (uint i = 0; i < array_length; i++)
{
// code that does not modify length of `array`
}
}
}
Cache the lengths of storage arrays if they are used and not modified in for
loops.
cache-array-length on Slither's wiki.