Scala 中是否有与 SuppressWarnings 等效的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3506370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:21:36  来源:igfitidea点击:

Is there an equivalent to SuppressWarnings in Scala?

scalasuppress-warnings

提问by BenjaminHymanman

I was wondering if scala had an equivalent to java's @SuppressWarningsthat can be applied to a function or whatever to ignore any deprecation warnings[1] that function emits?

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

1: Relevant warning in my case is: method stop in class Thread is deprecated: see corresponding Javadoc for more information.I am aware of the problems with stop however there are still some cases where due to legacy code we have to use it.

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

采纳答案by retronym

No, and an enhancement request [1] for such a feature was closed as wontfix.

不,并且针对此类功能的增强请求 [1] 已关闭为wontfix

I agree it would be useful. I expect that the Scala core team aren't against the idea, but they have finite resources and many higher priorities.

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

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

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

回答by ghik

There is a simple compiler plugin for this: silencer(a bit shameless plug)

为此有一个简单的编译器插件:消音器(有点不要脸的插件)

回答by Mario Galic

Scala 2.13.2 provides @nowarnannotation developed on the basis of ghik's silencer, for example

Scala 2.13.2提供@nowarn了在ghik的基础上开发的注解silencer,例如

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

raises no warnings, whilst

不发出警告,而

def t = { 0; 1 }

gives

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

回答by Guillaume Massé

Here is how to suppress all warnings in sbt:

以下是在 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("3c")
        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 = ()
    }
)