Skip to content

Commit

Permalink
support RemoveUnused on Scala 3.3.0-RC2
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Jan 29, 2023
1 parent ca66e1d commit 37cd1ef
Show file tree
Hide file tree
Showing 20 changed files with 21 additions and 41 deletions.
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ lazy val testsInput = projectMatrix
noPublishAndNoMima,
scalacOptions ~= (_.filterNot(_ == "-Yno-adapted-args")),
scalacOptions ++= warnAdaptedArgs.value, // For NoAutoTupling
scalacOptions ++= warnUnusedImports.value, // For RemoveUnused
scalacOptions ++= warnUnused.value, // For RemoveUnusedTerms
scalacOptions += "Wunused", // For RemoveUnusedTerms
logLevel := Level.Error, // avoid flood of compiler warnings
libraryDependencies ++= testsDependencies.value,
coverageEnabled := false
Expand Down
9 changes: 3 additions & 6 deletions docs/rules/RemoveUnused.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ example diff from running `sbt "scalafix RemoveUnused"`.

To use this rule:

- Enable the Scala compiler option `-Ywarn-unused` (or `-Wunused` in 2.13). In
sbt, this is done with `scalacOptions += "-Ywarn-unused"`.
- Enable the Scala compiler option `-Wunused`. In sbt, this is done with
`scalacOptions += "-Wunused"`.
- Disable `-Xfatal-warnings` if you have it enabled. This is required so the
compiler warnings do not fail the build before running Scalafix. If you are
running 2.13.2 or later, you may keep `-Xfatal-warnings` by modifying how
specific warnings are handled via `scalacOptions += "-Wconf:cat=unused:info"`.
- This rule **can't work** yet on Scala 3 projects since the compiler option `warn-unused`
is not yet available in Scala 3. You need to remove `RemoveUnused`
from `.scalafix.conf` for Scala 3 projects.

## Examples

Expand Down Expand Up @@ -146,4 +143,4 @@ Enable or disable specific `unused' warnings
params Enable -Ywarn-unused:explicits,implicits.
linted -Xlint:unused.
Default: All choices are enabled by default.
```
```
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.util.Try
object Dependencies {
val scala212 = "2.12.17"
val scala213 = "2.13.10"
val scala3 = "3.2.1"
val scala3 = "3.3.0-RC2"

val buildScalaVersions = Seq(scala212, scala213, scala3)
val testTargetScalaVersions = Seq(scala212, scala213, scala3)
Expand Down
7 changes: 1 addition & 6 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
scalaVersion.value.startsWith("2.12")
}
lazy val warnUnusedImports = Def.setting {
if (isScala3.value) Nil
else if (isScala213.value) Seq("-Wunused:imports")
if (isScala213.value || isScala3.value) Seq("-Wunused:imports")
else Seq("-Ywarn-unused-import")
}
lazy val warnUnused = Def.setting {
if (isScala2.value) Seq("-Ywarn-unused")
else Nil
}
lazy val targetJvm = Def.setting {
if (isScala3.value) Seq("-Xtarget:8")
else if (isScala213.value) Seq("-release", "8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RemoveUnused(config: RemoveUnusedConfig)
def this() = this(RemoveUnusedConfig.default)

override def description: String =
"Removes unused imports and terms that reported by the compiler under -Ywarn-unused"
"Removes unused imports and terms that reported by the compiler under -Yunused"
override def isRewrite: Boolean = true

private def warnUnusedPrefix = List("-Wunused", "-Ywarn-unused")
Expand All @@ -36,17 +36,18 @@ class RemoveUnused(config: RemoveUnusedConfig)
warnUnusedPrefix.exists(prefix => option.startsWith(prefix)) ||
warnUnusedString.contains(option)
)
if (config.scalaVersion.startsWith("3"))
Configured.error(
"This rule is specific to Scala 2, because the compiler option `-Ywarn-unused` is not available yet in scala 3 " +
"To fix this error, remove RemoveUnused from .scalafix.conf"
)
else if (!hasWarnUnused) {
Configured.error(
s"""|The Scala compiler option "-Ywarn-unused" is required to use RemoveUnused.
|To fix this problem, update your build to use at least one Scala compiler
|option like -Ywarn-unused, -Xlint:unused (2.12.2 or above), or -Wunused (2.13 only)""".stripMargin
)
if (!hasWarnUnused) {
if (config.scalaVersion.startsWith("3"))
Configured.error(
"""|The Scala compiler option "-Ywarn-unused" (available from Scala 3.3.0-RC2) is
|required to use RemoveUnused. To fix this problem, update your build to add it.""".stripMargin
)
else
Configured.error(
"""|A Scala compiler option is required to use RemoveUnused. To fix this problem,
|update your build to add either -Ywarn-unused, -Xlint:unused (2.12.2 or above),
|or -Wunused (2.13).""".stripMargin
)
} else {
config.conf
.getOrElse("RemoveUnused")(this.config)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package test.removeUnused

object RemoveUnusedTerms {

def foo {
def foo = {
val a = "unused"
val aa = println(5)
var b = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test.removeUnused

object RemoveUnusedTerms {

def foo {
def foo = {

println(5)

Expand Down

0 comments on commit 37cd1ef

Please sign in to comment.