Solidity: Designating Unused Variables To Avoid Warnings

Created on 25 Feb 2019  路  9Comments  路  Source: ethereum/solidity

Abstract

Under certain circumstances, variables must be declared even though there is no use for them. It should be possible to designate these variables as unused to avoid the "Unused local variable" compiler warnings.

Motivation

Solidity functions can return more than one variable. In a situation where a contract only cares about one of the returned variables, it is still necessary to declare and set the other variables. Because these variables are declared but not used, the compiler raises an Unused local variable warning. Given that I did not specify the interface for the function I'm calling, there's no way for me to avoid this warning.

Specification

In Go (where unused variables produce an error, rather than a warning), it is allowed to assign an unused variable to _ to avoid the unused variable error. I would propose that any unused variables assigned to the variable name _ simply be ignored by the compiler and no warning should be raised.

Backwards Compatibility

The only potential backward compatibility issue is that in cases where someone declares a variable named _ intending to use it will no longer get the backward compatibility warning. It shouldn't change the compiled bytecode, just the warnings generated by the compiler.

Most helpful comment

Oh I'm so sorry, I did not read your description thoroughly enough. You can just use (bool success,) = ... in that case. The recommendation is not provided because it is a local variable - which does not have to be declared at all, as opposed to e.g. a function parameter.

All 9 comments

The recommended way to avoid the warning is to remove the name of the variable and only leave the type. Can you elaborate on why this is not a sufficient solution for you?

I was unaware that's an option. Is it documented somewhere? Googling "solidity unused variable" I don't find that recommendation anywhere.

It is part of the error message itself.

The error message I see is:

> project/contracts/Account.sol:72:22: Warning: Unused local variable.
      (bool success, bytes memory _) = batch[i].target.call.value(batch[i].value)(batch[i].data);
                     ^------------^

I don't see any clue in here that I could avoid the warning by just specifying the type.

Additionally, if I try not naming the variable, I get:

ParserError: Expected identifier but got ')'
      (bool success, bytes) = batch[i].target.call.value(batch[i].value)(batch[i].data);
                          ^

Which version of the compiler are you using?

Solidity - 0.5.3 (solc-js)

Oh I'm so sorry, I did not read your description thoroughly enough. You can just use (bool success,) = ... in that case. The recommendation is not provided because it is a local variable - which does not have to be declared at all, as opposed to e.g. a function parameter.

Alright, that works. Aside from maybe updating documentation / warnings to make this clearer, this can be closed.

Created https://github.com/ethereum/solidity/issues/6162 to track improvement of the error message. Thanks!

Was this page helpful?
0 / 5 - 0 ratings