-Xfatal-warnings 下不会产生警告

No warnings can be incurred under -Xfatal-warnings

我正在尝试将包导入 sbt console,如下所示:

scala> import cats.instances.string
<console>:11: warning: Unused import
       import cats.instances.string
                             ^
error: No warnings can be incurred under -Xfatal-warnings. 

你可以看到,我收到一条错误消息。

build.sbt的内容是:

scalaVersion := "2.12.8"

scalacOptions ++= Seq(
  "-encoding", "UTF-8",   // source files are in UTF-8
  "-deprecation",         // warn about use of deprecated APIs
  "-unchecked",           // warn about unchecked type parameters
  "-feature",             // warn about misused language features
  "-language:higherKinds",// allow higher kinded types without `import scala.language.higherKinds`
  "-Xlint",               // enable handy linter warnings
  "-Xfatal-warnings",     // turn compiler warnings into errors
  "-Ypartial-unification" // allow the compiler to unify type constructors of different arities
)

libraryDependencies += "org.typelevel" %% "cats-core" % "1.4.0"
libraryDependencies += "org.tpolecat" %% "atto-core"    % "0.6.5"
libraryDependencies += "org.tpolecat" %% "atto-refined" % "0.6.5"

addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.3")

我做错了什么?

这种情况下的最佳解决方案是从用于控制台的 Scala 选项中删除 -Xlint

scalaVersion := "2.12.8"
scalacOptions ++= Seq(
  "-Xlint",
  "-Xfatal-warnings"
)

scalacOptions in (Compile, console) ~= {
  _.filterNot(Set("-Xlint"))
}

libraryDependencies += "org.typelevel" %% "cats-core" % "1.6.0"

使用此配置,您项目中的任何源代码都将使用 -Xlint 进行编译,但在 REPL 中解释的任何代码都不会。这通常正是您想要的:对您的项目代码进行最彻底的安全检查,但在 REPL 中进行实验更加灵活。