我怎样才能让scalac告诉我是否有未使用的变量?

时间:2012-09-12 09:39:14

标签: scala lint

我刚刚发现我的代码中有一个错误,我在其中创建了一个新变量,但后来却未能真正使用它。

我认为scalac会告诉我我的新变量未使用,但似乎并非如此,并且在少量谷歌搜索/手册页之后,我找不到任何关于启用警告的信息。

如何启用此类警告?

3 个答案:

答案 0 :(得分:7)

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

讨论结果:IDE的任务是考虑到这一点(到目前为止它们没有或至少不是非常详尽)

主要论点似乎是scala编译器因为速度慢而受到批评,因此在顶部添加更多东西可能不是一个好主意。

答案 1 :(得分:2)

编译器现在可以警告您未使用的私有变量,因为d0c4be6861。这是-Xlint。请参阅相关bug report中的讨论。如果对-Xlint的提及不熟悉,答案就在scalac手册页中。

答案 2 :(得分:2)

从scalac 2.12开始,您现在可以使用-Ywarn-unused:locals。如果你并不仅仅意味着局部变量,那么还有其他选择:

$ 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.
相关问题