From 37cd1ef157dd09158f1f43563a5366f0136384fc Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Sun, 29 Jan 2023 01:30:01 +0100 Subject: [PATCH] support RemoveUnused on Scala 3.3.0-RC2 --- build.sbt | 3 +-- docs/rules/RemoveUnused.md | 9 +++---- project/Dependencies.scala | 2 +- project/ScalafixBuild.scala | 7 +----- .../scalafix/internal/rule/RemoveUnused.scala | 25 ++++++++++--------- .../removeUnused/RemoveUnusedParams.scala | 12 --------- .../removeUnused/RemoveUnusedImports.scala | 0 .../removeUnused/RemoveUnusedImports2.scala | 0 .../RemoveUnusedImportsCommas.scala | 0 .../RemoveUnusedImportsRename.scala | 0 .../removeUnused/RemoveUnusedParams.scala | 0 .../removeUnused/RemoveUnusedStatements.scala | 0 .../test/removeUnused/RemoveUnusedTerms.scala | 2 +- .../removeUnused/RemoveUnusedImports.scala | 0 .../removeUnused/RemoveUnusedImports2.scala | 0 .../RemoveUnusedImportsCommas.scala | 0 .../RemoveUnusedImportsRename.scala | 0 .../removeUnused/RemoveUnusedParams.scala | 0 .../removeUnused/RemoveUnusedStatements.scala | 0 .../test/removeUnused/RemoveUnusedTerms.scala | 2 +- 20 files changed, 21 insertions(+), 41 deletions(-) delete mode 100644 scalafix-tests/input/src/main/scala-2.13/test/removeUnused/RemoveUnusedParams.scala rename scalafix-tests/input/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImports.scala (100%) rename scalafix-tests/input/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImports2.scala (100%) rename scalafix-tests/input/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImportsCommas.scala (100%) rename scalafix-tests/input/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImportsRename.scala (100%) rename scalafix-tests/input/src/main/{scala-2.12 => scala}/test/removeUnused/RemoveUnusedParams.scala (100%) rename scalafix-tests/input/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedStatements.scala (100%) rename scalafix-tests/input/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedTerms.scala (96%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImports.scala (100%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImports2.scala (100%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImportsCommas.scala (100%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedImportsRename.scala (100%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedParams.scala (100%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedStatements.scala (100%) rename scalafix-tests/output/src/main/{scala-2 => scala}/test/removeUnused/RemoveUnusedTerms.scala (93%) diff --git a/build.sbt b/build.sbt index 25e2e950c6..758abf72ff 100644 --- a/build.sbt +++ b/build.sbt @@ -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 diff --git a/docs/rules/RemoveUnused.md b/docs/rules/RemoveUnused.md index 1df088933f..f3f9dcafd8 100644 --- a/docs/rules/RemoveUnused.md +++ b/docs/rules/RemoveUnused.md @@ -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 @@ -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. -``` \ No newline at end of file +``` diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 686d78b72c..f96de2ff0c 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -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) diff --git a/project/ScalafixBuild.scala b/project/ScalafixBuild.scala index 28b4ea3a3a..4161b91798 100644 --- a/project/ScalafixBuild.scala +++ b/project/ScalafixBuild.scala @@ -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") diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala index 2583ddc6c4..59822f046b 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala @@ -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") @@ -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) diff --git a/scalafix-tests/input/src/main/scala-2.13/test/removeUnused/RemoveUnusedParams.scala b/scalafix-tests/input/src/main/scala-2.13/test/removeUnused/RemoveUnusedParams.scala deleted file mode 100644 index 16e3a62ca2..0000000000 --- a/scalafix-tests/input/src/main/scala-2.13/test/removeUnused/RemoveUnusedParams.scala +++ /dev/null @@ -1,12 +0,0 @@ -/* -rule = RemoveUnused - */ -package test.removeUnused - -object UnusedParams { - val f: String => Unit = unused => println("f") - val ff = (unused: String) => println("f") - val fs = (used: String, unused: Long) => println(used) - def g(x: String => Unit): Unit = ??? - g{implicit string => println("g")} -} diff --git a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImports.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImports.scala similarity index 100% rename from scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImports.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImports.scala diff --git a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImports2.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImports2.scala similarity index 100% rename from scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImports2.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImports2.scala diff --git a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImportsCommas.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImportsCommas.scala similarity index 100% rename from scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImportsCommas.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImportsCommas.scala diff --git a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImportsRename.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImportsRename.scala similarity index 100% rename from scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedImportsRename.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedImportsRename.scala diff --git a/scalafix-tests/input/src/main/scala-2.12/test/removeUnused/RemoveUnusedParams.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedParams.scala similarity index 100% rename from scalafix-tests/input/src/main/scala-2.12/test/removeUnused/RemoveUnusedParams.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedParams.scala diff --git a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedStatements.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedStatements.scala similarity index 100% rename from scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedStatements.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedStatements.scala diff --git a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedTerms.scala b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala similarity index 96% rename from scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedTerms.scala rename to scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala index ecc3ce1e6a..98b963a8ca 100644 --- a/scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedTerms.scala +++ b/scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala @@ -5,7 +5,7 @@ package test.removeUnused object RemoveUnusedTerms { - def foo { + def foo = { val a = "unused" val aa = println(5) var b = 0 diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImports.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImports.scala similarity index 100% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImports.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImports.scala diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImports2.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImports2.scala similarity index 100% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImports2.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImports2.scala diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImportsCommas.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImportsCommas.scala similarity index 100% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImportsCommas.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImportsCommas.scala diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImportsRename.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImportsRename.scala similarity index 100% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedImportsRename.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedImportsRename.scala diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedParams.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedParams.scala similarity index 100% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedParams.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedParams.scala diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedStatements.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedStatements.scala similarity index 100% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedStatements.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedStatements.scala diff --git a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedTerms.scala b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala similarity index 93% rename from scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedTerms.scala rename to scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala index c7c49a7f3a..5e19295342 100644 --- a/scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedTerms.scala +++ b/scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala @@ -2,7 +2,7 @@ package test.removeUnused object RemoveUnusedTerms { - def foo { + def foo = { println(5)