Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get scalac to tell me if I have unused variables?

Tags:

lint

scala

I just noticed a bug in my code where I created a new variable, but then failed to actually use it.

I assumed that scalac would have told me that my new variable was unused, but this didn't seem to be the case, and after a small amount of googling / man page, I couldn't find anything about enabling warnings.

What can I do to enable such warnings?

like image 966
Squidly Avatar asked Sep 12 '12 09:09

Squidly


People also ask

Why are unused variables bad?

Unused variables are a waste of space in the source; a decent compiler won't create them in the object file. Unused parameters when the functions have to meet an externally imposed interface are a different problem; they can't be avoided as easily because to remove them would be to change the interface.


1 Answers

As of scalac 2.12, you can now use -Ywarn-unused:locals. In case you didn't mean just local variables, there are other options too:

$ scalac -Ywarn-unused:help
Enable or disable specific `unused' warnings
  imports    Warn if an import selector is not referenced.
  patvars    Warn if a variable bound in a pattern is unused.
  privates   Warn if a private member is unused.
  locals     Warn if a local definition is unused.
  explicits  Warn if an explicit parameter is unused.
  implicits  Warn if an implicit parameter is unused.
  params     Enable -Ywarn-unused:explicits,implicits.
  linted     -Xlint:unused.
Default: All choices are enabled by default.
like image 84
aij Avatar answered Oct 12 '22 19:10

aij