Is there an equivalent to SuppressWarnings in Scala?

ScalaSuppress Warnings

Scala Problem Overview


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.

Scala Solutions


Solution 1 - Scala

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

Solution 2 - Scala

EDIT: You should use @nowarn


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

Solution 3 - Scala

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

Solution 4 - Scala

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 = ()
    }
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenjaminJackmanView Question on Stackoverflow
Solution 1 - ScalaretronymView Answer on Stackoverflow
Solution 2 - ScalaghikView Answer on Stackoverflow
Solution 3 - ScalaMario GalicView Answer on Stackoverflow
Solution 4 - ScalaGuillaume MasséView Answer on Stackoverflow