如果我有未使用的变量,如何让scalac告诉我



我刚刚注意到我的代码中有一个错误,我创建了一个新变量,但后来没有真正使用它。

我以为scalac会告诉我我的新变量未使用,但事实似乎并非如此,在谷歌上搜索了少量/主页后,我找不到任何关于启用警告的信息。

我该怎么做才能启用此类警告?

这些内容刚刚在scala用户邮件列表中讨论过。

讨论的结果:这被认为是IDE的任务(到目前为止,他们还没有,或者至少不是很详尽)

主要的论点似乎是scala编译器已经被批评为速度慢,所以在上面添加更多的东西可能不是一个好主意。

从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.

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

最新更新