Scala 中是否有相当于 SuppressWarnings 的功能?

发布于 2024-09-14 22:32:59 字数 301 浏览 4 评论 0原文

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

1:在我的情况下,相关警告是:Thread类中的方法停止已被弃用:请参阅相应的Javadoc以获取更多信息。我知道停止的问题,但仍然存在一些情况,由于遗留代码我们必须使用它。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好听的两个字的网名 2024-09-21 22:32:59

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

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

更新:此功能最终在 2020 年 4 月 22 日的 scala 2.13.2 版本中实现,请参阅此答案

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

No, and an enhancement request [1] for such a feature was closed as 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.

update: this feature was eventually implemented in scala 2.13.2 release on 2020-04-22, see this answer

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

白色秋天 2024-09-21 22:32:59

编辑:您应该使用@nowarn


为此有一个简单的编译器插件:消音器(有点无耻的插件)

EDIT: You should use @nowarn


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

揽月 2024-09-21 22:32:59

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

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 }
            ^

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

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 }
            ^
行至春深 2024-09-21 22:32:59

以下是如何抑制 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 = ()
    }
)

Here is how to suppress all warnings in 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 = ()
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文