Scala中有相当于SuppressWarnings的内容吗?

时间:2010-08-17 19:51:54

标签: scala suppress-warnings

我想知道scala是否有相当于java的@SuppressWarnings可以应用于函数或者忽略任何函数发出的弃用警告[1]?

1:我的案例中的相关警告是:method stop in class Thread is deprecated: see corresponding Javadoc for more information.我知道停止的问题,但仍有一些情况由于遗留代码我们必须使用它。

4 个答案:

答案 0 :(得分:25)

否,此类功能的增强请求[1]已关闭为wontfix

我同意这会很有用。我希望Scala核心团队不反对这个想法,但他们拥有有限的资源和更高的优先级。

[1] https://issues.scala-lang.org/browse/SI-1781

答案 1 :(得分:21)

有一个简单的编译器插件:silencer(有点无耻的插件)

答案 2 :(得分:15)

例如,Scala 2.13.2提供了基于ghik的silencer开发的@nowarn注释

import scala.annotation.nowarn
def t = { 0: @nowarn; 1 }

不发出警告,而

def t = { 0; 1 }

给予

warning: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
  def t = { 0; 1 }
            ^

答案 3 :(得分:-2)

以下是禁止显示sbt中所有警告的方法:

import sbt._
import Keys._
import KeyRanks.DTask
import xsbti.{Reporter, Problem, Position, Severity}

private lazy val compilerReporter = TaskKey[xsbti.Reporter](
  "compilerReporter",
  "Experimental hook to listen (or send) compilation failure messages.",
  DTask
)

val ignoreWarnings = Seq(
  compilerReporter in (Compile, compile) :=
    new xsbti.Reporter {
      private val buffer = collection.mutable.ArrayBuffer.empty[Problem]
      def reset(): Unit = buffer.clear()
      def hasErrors: Boolean = buffer.exists(_.severity == Severity.Error)
      def hasWarnings: Boolean = buffer.exists(_.severity == Severity.Warn)
      def printSummary(): Unit = {

        print("\033c")
        if (problems.nonEmpty) {
          problems.foreach{ p =>
            println("=====================================================")
            println(p.position)
            println(p.message)
            println()
            println()
          }
        }
      }
      def problems: Array[Problem] = buffer.toArray

      def log(problem: Problem): Unit = {
        if (problem.severity == Severity.Error) {
          buffer.append(problem)
        }
      }
      def log(pos: Position, msg: String, sev: Severity): Unit = {
        log(new Problem {
          def category: String = "foo"
          def severity: Severity = sev
          def message: String = msg
          def position: Position = pos
        })
      }
      def comment(pos: xsbti.Position, msg: String): Unit = ()
    }
)
相关问题