0

As soon as I upgraded the code from solidity version 0.5.2 to 0.5.3, it showing warning in my for loop.

Warning: Unreachable code.
for(uint256 i=0; i < totalHoldTransactions; i++ ){
                                            ^-^

This SO article which gives idea in other programming languages. But I am still not sure why that increment variable is unreachable!

Any ideas will be appreciated. Thanks.

2
  • Maybe your totalHoldTransactions is 0 (or left uninitialized)? Anyway, it would help if you post more of your code. Jan 23, 2019 at 13:22
  • Thanks Lauri.. It should work even that is zero.. only issue was the return statement which cased loop to bounce.. and make increment variable to be unreachable.. Jan 23, 2019 at 13:25

2 Answers 2

1

After lots of research the culprit was the return statement inside the loop. So to anyone has this issue, you need to move any code outside of the for loop which causes it to bounce.

 for(uint256 i=0; i < totalHoldTransactions; i++ ){
       //other code..
       if(i >= 10){ break; }

       return true;  //this was the issue. I moved it to outside of loop and everything worked!!

    }
2
  • The problem that I see is that apparently the compiler is right. How should you reach that i++?
    – Rick Park
    Jan 23, 2019 at 22:10
  • Yes @Rick.. You are right.. I was making Silly mistake ;) Jan 24, 2019 at 1:13
0

I'm using v 0.8.17, and I faced the same warning.

This is the answer: just continue.

It will clear off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.