Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about unused local variables #718

Closed
4 tasks done
ethers opened this issue Jul 13, 2016 · 16 comments
Closed
4 tasks done

Warn about unused local variables #718

ethers opened this issue Jul 13, 2016 · 16 comments

Comments

@ethers
Copy link
Member

ethers commented Jul 13, 2016

  • warn about unused arguments
  • warn about local variables that are never accessed as rvalues
  • warn about return values that are never assigned
  • warn about unnamed return values in case there is only a return statement without arguments.

Related to #708

    function foo() returns (uint r) {
        // bunch of code and r is never set
        return 10;
    }

Can the compiler force an error that r is unused?

Even worse is if an explicit return was forgotten:

    function foo() returns (uint r) {
        // bunch of code and r is never set
    }

0 would always be returned without any warning.

The code would be much easier to read as:

    function foo() returns (uint) {
        // bunch of code
        return 10;
    }
@Denton-L
Copy link
Contributor

I don't think this should throw an error because errors should only be reserved for when the compiler can't go on.

However, you bring up a good point. I believe that there should be warnings for unused local variables in general. This is a common thing that most other compilers will warn about.

@redsquirrel
Copy link
Contributor

Related #677

@ethers ethers changed the title Force error when return variable is unused Force error when named return variable is unused Jul 18, 2016
@chriseth chriseth changed the title Force error when named return variable is unused Warn about unused local variables Aug 11, 2016
@chriseth chriseth added this to the 2-non-breaking-saftey-changes milestone Aug 11, 2016
@chriseth chriseth added the soon label Aug 12, 2016
@axic
Copy link
Member

axic commented Nov 14, 2016

Should this be extended to unreferenced, non-public storage variables?

@ethers
Copy link
Member Author

ethers commented Nov 15, 2016

Should this be extended to unreferenced, non-public storage variables?

Yes I think so. If it's all fixable in the same commit then this issue is fine, otherwise a separate issue could be better and I can improve the title on this one, to mention the unused variable in the return declaration.

@chriseth
Copy link
Contributor

Unreferenced local variables have their use in base contracts.

@roadriverrail
Copy link
Contributor

I started to look into this, focusing on the local variables case first. I was able, I think, to ensure the expression compiler marks every variable it uses. It looks like I'd then need to actually implement a new AST visitor to sweep through the AST and make note of all the variables not marked as used by the ExpressionCompiler. Before I go crazy writing a new visitor, though, I thought it might be prudent to ask if there's a simpler way I didn't notice.

@chriseth
Copy link
Contributor

That sounds about right, great!
The expression compiler is not the correct place to add this check, all these checks should be done before the code generation phase.

I guess the correct class to add these two passes is StaticAnalyzer. You might want to refactor it so that the different aspects of the analysis are better separated, but I guess it is still fine that way for now.

@roadriverrail
Copy link
Contributor

roadriverrail commented Jan 25, 2017 via email

@roadriverrail
Copy link
Contributor

I've got a naive implementation of this, I think. Played around with it on some test code and it seems to be able to detect unused locals, unused parameters, and unused named return variables. I see you want warnings about unnamed return variables, which could be added, since I currently suppress them by refusing to track an unnamed variable. All of the above features were pretty easy because the language currently treats all parameters and return variables as just more local variables.

As for warning if you never assign to a named return variable, I don't think that'd be too hard to track now that I've got a feel for things. It seems like a logically different concern from unused variable tracking, so do we want that put in the same commit or a different one?

@chriseth
Copy link
Contributor

chriseth commented May 3, 2017

@roadriverrail in my view, this is fully resolved although your PR states that it only partially solves this issue. Please reopen if you think something is still missing.

@chriseth chriseth closed this as completed May 3, 2017
@chriseth chriseth removed the soon label May 3, 2017
@pash7ka
Copy link

pash7ka commented Aug 28, 2017

There has to be some way to dissable this warnings. Overwise we have issues like this: OpenZeppelin/openzeppelin-contracts#367
Also Ethereum Wallet 0.9.0 does not compile contracts with warnings.

@axic
Copy link
Member

axic commented Aug 28, 2017

@pash7ka that has ben raised on Mist ("Ethereum Wallet") already: ethereum/mist#2797

@pash7ka
Copy link

pash7ka commented Aug 28, 2017

@axic That issue in Mist is about handling warnings. But there are usecases where having unused variables in a function declaration is not a bug, but a part of contract's public API.
There has to be some way to declare it to compiler so that no warning is raised.

@axic
Copy link
Member

axic commented Aug 28, 2017

One can always use uint256 /*value*/ if it needs to be stated.

I think also we should lift the unused variable requirement for interfaces:

interface I {
  function transfer(address from, uint amount);
}

@chriseth
Copy link
Contributor

@axic yes, unused variables should not be warned about if the function does not have an implementation (also outside of interfaces).

@pash7ka
Copy link

pash7ka commented Aug 28, 2017

@axic Thank you, uint256 /*value*/ is fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants