20,000 USDC
View results
Submission Details
Severity: gas
Valid

++i/i++ SHOULD BE UNCHECKED{++i}/UNCHECKED{i++} WHEN IT IS NOT POSSIBLE FOR THEM TO OVERFLOW, AS IS THE CASE WHEN USED IN FOR- AND WHILE-LOOPS

Increments can be unchecked in loops

Summary
Unchecked operations as the ++i on for loops are cheaper than checked one.

Details
In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each
iteration, but at the cost of some code readability, as this uncheck cannot be made inline..

The code would go from:

for (uint256 i; i < numIterations; i++) {
// ...
}

to

for (uint256 i; i < numIterations;) {
// ...
unchecked { ++i; }
}

The risk of overflow is inexistent for a uint256 here.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.