Skip to content

Commit

Permalink
support RemoveUnused on Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Jun 4, 2023
1 parent dc2b126 commit fec31ed
Show file tree
Hide file tree
Showing 30 changed files with 70 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .scalafix.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ExplicitResultTypes {
OrganizeImports {
groupedImports = Explode
expandRelative = true
removeUnused = true # done already by RemoveUnused rule
removeUnused = true
groups = [
"re:javax?\\."
"scala."
Expand Down
4 changes: 1 addition & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ lazy val input = projectMatrix
noPublishAndNoMima,
scalacOptions ~= (_.filterNot(_ == "-Yno-adapted-args")),
scalacOptions ++= warnAdaptedArgs.value, // For NoAutoTupling
scalacOptions ++= warnUnusedImports.value, // For RemoveUnused
scalacOptions ++= warnUnused.value, // For RemoveUnusedTerms
scalacOptions += warnUnused.value, // For RemoveUnusedTerms
logLevel := Level.Error, // avoid flood of compiler warnings
libraryDependencies ++= testsDependencies.value,
coverageEnabled := false
Expand All @@ -205,7 +204,6 @@ lazy val output = projectMatrix
.in(file("scalafix-tests/output"))
.settings(
noPublishAndNoMima,
scalacOptions --= warnUnusedImports.value,
libraryDependencies ++= testsDependencies.value,
coverageEnabled := false
)
Expand Down
70 changes: 51 additions & 19 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 `-Ywarn-unused` (2.12.x), `-Wunused`
(2.13.x), or `-Wunused:all` (>=3.3.0).
- 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 @@ -129,21 +126,56 @@ import scalafix.internal.rule._
println(scalafix.website.rule("RemoveUnused", RemoveUnusedConfig.default))
```

## -Ywarn-unused
## More granular scalac options

Consult `scala -Y` in the command-line for more information about using
`-Ywarn-unused`.
You may request more granular warnings to the compiler if you opt-out
from some rewrites in the rule configuration.

```
$ scala -Ywarn-unused:help
Enable or disable specific `unused' warnings
imports Warn if an import selector is not referenced.
patvars Warn if a variable bound in a pattern is unused.
privates Warn if a private member is unused.
locals Warn if a local definition is unused.
explicits Warn if an explicit parameter is unused.
implicits Warn if an implicit parameter is unused.
params Enable -Ywarn-unused:explicits,implicits.
linted -Xlint:unused.
$ scala212 -Wunused:help
Enable or disable specific `unused` warnings
imports Warn if an import selector is not referenced.
patvars Warn if a variable bound in a pattern is unused.
privates Warn if a private member is unused.
locals Warn if a local definition is unused.
explicits Warn if an explicit parameter is unused.
implicits Warn if an implicit parameter is unused.
synthetics Warn if a synthetic implicit parameter (context bound) is unused.
nowarn Warn if a @nowarn annotation does not suppress any warnings.
params Enable -Wunused:explicits,implicits,synthetics.
linted -Xlint:unused.
Default: All choices are enabled by default.
```
```

```
$ scala3 -W
...
-Wunused Enable or disable specific `unused` warnings
Choices :
- nowarn,
- all,
- imports :
Warn if an import selector is not referenced.
NOTE : overrided by -Wunused:strict-no-implicit-warn,
- privates :
Warn if a private member is unused,
- locals :
Warn if a local definition is unused,
- explicits :
Warn if an explicit parameter is unused,
- implicits :
Warn if an implicit parameter is unused,
- params :
Enable -Wunused:explicits,implicits,
- linted :
Enable -Wunused:imports,privates,locals,implicits,
- strict-no-implicit-warn :
Same as -Wunused:import, only for imports of explicit named
members.
NOTE : This overrides -Wunused:imports and NOT set by
-Wunused:all,
- unsafe-warn-patvars :
(UNSAFE) Warn if a variable bound in a pattern is unused.
This warning can generate false positive, as warning cannot be
suppressed yet.
```
3 changes: 1 addition & 2 deletions docs/users/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pull request is tested on both Linux and Windows.

**Scala 2.12 and 2.13**

**Scala 3.x**: Scala 3 support is experimental and many built-in rules are not
supported.
**Scala 3.x** (3.3.1 for RemoveUnused)

## sbt

Expand Down
16 changes: 6 additions & 10 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,10 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
lazy val isScala212 = Def.setting {
scalaVersion.value.startsWith("2.12")
}
lazy val warnUnusedImports = Def.setting {
if (isScala3.value) Nil
else if (isScala213.value) Seq("-Wunused:imports")
else Seq("-Ywarn-unused-import")
}
lazy val warnUnused = Def.setting {
if (isScala2.value) Seq("-Ywarn-unused")
else Nil
if (isScala3.value) "-Wunused:all"
else if (isScala213.value) "-Wunused"
else "-Ywarn-unused"
}
lazy val targetJvm = Def.setting {
if (isScala3.value) Seq("-Xtarget:8")
Expand All @@ -80,13 +76,13 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
scalaXml +: otherLibs
}
lazy val compilerOptions = Def.setting(
warnUnusedImports.value ++
targetJvm.value ++
targetJvm.value ++
Seq(
"-encoding",
"UTF-8",
"-feature",
"-unchecked"
"-unchecked",
warnUnused.value
)
)

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 -Wunused"
override def isRewrite: Boolean = true

private def warnUnusedPrefix = List("-Wunused", "-Ywarn-unused")
Expand All @@ -36,16 +36,11 @@ class RemoveUnused(config: RemoveUnusedConfig)
warnUnusedPrefix.exists(prefix => option.startsWith(prefix)) ||
warnUnusedString.contains(option)
)
if (config.scalaVersion.startsWith("3"))
if (!hasWarnUnused) {
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
"""|A Scala compiler option is required to use RemoveUnused. To fix this problem,
|update your build to add -Ywarn-unused (2.12.x), -Wunused (2.13.x), or
|-Wunused:all (>=3.3.0)""".stripMargin
)
} else {
config.conf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ case class RemoveUnusedConfig(
privates: Boolean = true,
@Description("Remove unused local definitions")
locals: Boolean = true,
@Description(
"Remove unused pattern match variables (compatible with Scala 2.12 and 2.13)"
)
@Description("Remove unused pattern match variables")
patternvars: Boolean = true,
@Description(
"Remove unused function parameters (compatible with Scala 2.12 and 2.13)"
)
@Description("Remove unused function parameters")
params: Boolean = true
)

Expand Down

This file was deleted.

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 @@ -43,7 +43,7 @@ trait BaseCliSuite extends AnyFunSuite with DiffAssertions {
val ps = new PrintStream(new ByteArrayOutputStream())

val removeImportsPath: RelativePath =
RelativePath("scala-2/test/removeUnused/RemoveUnusedImports.scala")
RelativePath("scala/test/removeUnused/RemoveUnusedImports.scala")
val explicitResultTypesPath: RelativePath =
RelativePath(
"scala-2/test/explicitResultTypes/ExplicitResultTypesBase.scala"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,4 @@ class CliSemanticSuite extends BaseCliSuite {
assert(exit.is(ExitStatus.MissingSemanticdbError))
}

checkSemantic(
name = "ScalaVersion Scala3",
args = Array(
"--classpath",
defaultClasspath,
"--scalaVersion",
"3.0.0-RC3"
),
expectedExit = ExitStatus.CommandLineError
)

}
Loading

0 comments on commit fec31ed

Please sign in to comment.