How can I get scalac to tell me if I have unused v

2019-03-25 07:36发布

问题:

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?

回答1:

This stuff was just now discussed on the scala user mailing list.

Result of the discussion: It's considererd task of IDE to do that (so far they don't or at least not very exhaustive)

The main argument seems to be that the scala compiler is already criticised for being slow, so it might not be a good idea to add even more stuff on top.



回答2:

The compiler can now warn you of unused private variables, since d0c4be6861. This is under -Xlint. See the discussion in the associated bug report. If the mention of -Xlint is unfamiliar, the answer is in the scalac man page.



回答3:

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.


标签: scala lint