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

蹲街弑〆低调 提交于 2020-01-01 04:21:08

问题


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:


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.



回答3:


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.



来源:https://stackoverflow.com/questions/12385293/how-can-i-get-scalac-to-tell-me-if-i-have-unused-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!