Solidity – Handling abi.decode() and Unused Variables

abisolcsolidity

I am decoding bytes as the following:

      // Decode Solidity tightly packed arguments
      (uint8 _, uint128 stakeId, address behalf) = abi.decode(userData, (uint8, uint128, address));  // solhint-disable-line

I get a compiler warning:

Variable '_' is declared but never used.

What would be the right way to suppress this warning beautifully? I know how to do this for function arguments, but not sure how what is a good practice with abi.decode.

The obvious way does not work:

/Users/moo/code/dawn-erc20/contracts/Staking.sol:255:23: ParserError: Expected ',' but got identifier
      (uint8, uint128 stakeId, address behalf) = abi.decode(userData, (uint8, uint128, address));  // solhint-disable-line

Best Answer

You could get rid of it in the function-call as well, i.e.:

(uint128 stakeId, address behalf) = abi.decode(userData, (uint128, address));

But I suppose that would just beat your purpose to begin with.

So the quickest workaround which comes to mind is to just add a dummy reference:

(uint8 _, uint128 stakeId, address behalf) = abi.decode(userData, (uint8, uint128, address));
_; // a dummy reference to an unused local variable
Related Topic