Skip to content

Commit 0207e37

Browse files
committed
support RemoveUnused on Scala 3
1 parent 4b5ea10 commit 0207e37

29 files changed

+69
-222
lines changed

.scalafix.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ExplicitResultTypes {
99
OrganizeImports {
1010
groupedImports = Explode
1111
expandRelative = true
12-
removeUnused = true # done already by RemoveUnused rule
12+
removeUnused = true
1313
groups = [
1414
"re:javax?\\."
1515
"scala."

build.sbt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ lazy val input = projectMatrix
180180
noPublishAndNoMima,
181181
scalacOptions ~= (_.filterNot(_ == "-Yno-adapted-args")),
182182
scalacOptions ++= warnAdaptedArgs.value, // For NoAutoTupling
183-
scalacOptions ++= warnUnusedImports.value, // For RemoveUnused
184-
scalacOptions ++= warnUnused.value, // For RemoveUnusedTerms
183+
scalacOptions += warnUnused.value, // For RemoveUnusedTerms
185184
logLevel := Level.Error, // avoid flood of compiler warnings
186185
libraryDependencies ++= testsDependencies.value,
187186
coverageEnabled := false
@@ -194,7 +193,6 @@ lazy val output = projectMatrix
194193
.in(file("scalafix-tests/output"))
195194
.settings(
196195
noPublishAndNoMima,
197-
scalacOptions --= warnUnusedImports.value,
198196
libraryDependencies ++= testsDependencies.value,
199197
coverageEnabled := false
200198
)

docs/rules/RemoveUnused.md

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ example diff from running `sbt "scalafix RemoveUnused"`.
1414

1515
To use this rule:
1616

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

2724
## Examples
2825

@@ -129,21 +126,56 @@ import scalafix.internal.rule._
129126
println(scalafix.website.rule("RemoveUnused", RemoveUnusedConfig.default))
130127
```
131128

132-
## -Ywarn-unused
129+
## More granular scalac options
133130

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

137134
```
138-
$ scala -Ywarn-unused:help
139-
Enable or disable specific `unused' warnings
140-
imports Warn if an import selector is not referenced.
141-
patvars Warn if a variable bound in a pattern is unused.
142-
privates Warn if a private member is unused.
143-
locals Warn if a local definition is unused.
144-
explicits Warn if an explicit parameter is unused.
145-
implicits Warn if an implicit parameter is unused.
146-
params Enable -Ywarn-unused:explicits,implicits.
147-
linted -Xlint:unused.
135+
$ scala212 -Wunused:help
136+
Enable or disable specific `unused` warnings
137+
imports Warn if an import selector is not referenced.
138+
patvars Warn if a variable bound in a pattern is unused.
139+
privates Warn if a private member is unused.
140+
locals Warn if a local definition is unused.
141+
explicits Warn if an explicit parameter is unused.
142+
implicits Warn if an implicit parameter is unused.
143+
synthetics Warn if a synthetic implicit parameter (context bound) is unused.
144+
nowarn Warn if a @nowarn annotation does not suppress any warnings.
145+
params Enable -Wunused:explicits,implicits,synthetics.
146+
linted -Xlint:unused.
148147
Default: All choices are enabled by default.
149-
```
148+
```
149+
150+
```
151+
$ scala3 -W
152+
...
153+
-Wunused Enable or disable specific `unused` warnings
154+
Choices :
155+
- nowarn,
156+
- all,
157+
- imports :
158+
Warn if an import selector is not referenced.
159+
NOTE : overrided by -Wunused:strict-no-implicit-warn,
160+
- privates :
161+
Warn if a private member is unused,
162+
- locals :
163+
Warn if a local definition is unused,
164+
- explicits :
165+
Warn if an explicit parameter is unused,
166+
- implicits :
167+
Warn if an implicit parameter is unused,
168+
- params :
169+
Enable -Wunused:explicits,implicits,
170+
- linted :
171+
Enable -Wunused:imports,privates,locals,implicits,
172+
- strict-no-implicit-warn :
173+
Same as -Wunused:import, only for imports of explicit named
174+
members.
175+
NOTE : This overrides -Wunused:imports and NOT set by
176+
-Wunused:all,
177+
- unsafe-warn-patvars :
178+
(UNSAFE) Warn if a variable bound in a pattern is unused.
179+
This warning can generate false positive, as warning cannot be
180+
suppressed yet.
181+
```

project/ScalafixBuild.scala

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
4646
lazy val isScala212 = Def.setting {
4747
scalaVersion.value.startsWith("2.12")
4848
}
49-
lazy val warnUnusedImports = Def.setting {
50-
if (isScala3.value) Nil
51-
else if (isScala213.value) Seq("-Wunused:imports")
52-
else Seq("-Ywarn-unused-import")
53-
}
5449
lazy val warnUnused = Def.setting {
55-
if (isScala2.value) Seq("-Ywarn-unused")
56-
else Nil
50+
if (isScala3.value) "-Wunused:all"
51+
else if (isScala213.value) "-Wunused"
52+
else "-Ywarn-unused"
5753
}
5854
lazy val targetJvm = Def.setting {
5955
if (isScala3.value) Seq("-Xtarget:8")
@@ -80,13 +76,13 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
8076
scalaXml +: otherLibs
8177
}
8278
lazy val compilerOptions = Def.setting(
83-
warnUnusedImports.value ++
84-
targetJvm.value ++
79+
targetJvm.value ++
8580
Seq(
8681
"-encoding",
8782
"UTF-8",
8883
"-feature",
89-
"-unchecked"
84+
"-unchecked",
85+
warnUnused.value
9086
)
9187
)
9288

scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RemoveUnused(config: RemoveUnusedConfig)
2626
def this() = this(RemoveUnusedConfig.default)
2727

2828
override def description: String =
29-
"Removes unused imports and terms that reported by the compiler under -Ywarn-unused"
29+
"Removes unused imports and terms that reported by the compiler under -Wunused"
3030
override def isRewrite: Boolean = true
3131

3232
private def warnUnusedPrefix = List("-Wunused", "-Ywarn-unused")
@@ -36,16 +36,11 @@ class RemoveUnused(config: RemoveUnusedConfig)
3636
warnUnusedPrefix.exists(prefix => option.startsWith(prefix)) ||
3737
warnUnusedString.contains(option)
3838
)
39-
if (config.scalaVersion.startsWith("3"))
39+
if (!hasWarnUnused) {
4040
Configured.error(
41-
"This rule is specific to Scala 2, because the compiler option `-Ywarn-unused` is not available yet in scala 3 " +
42-
"To fix this error, remove RemoveUnused from .scalafix.conf"
43-
)
44-
else if (!hasWarnUnused) {
45-
Configured.error(
46-
s"""|The Scala compiler option "-Ywarn-unused" is required to use RemoveUnused.
47-
|To fix this problem, update your build to use at least one Scala compiler
48-
|option like -Ywarn-unused, -Xlint:unused (2.12.2 or above), or -Wunused (2.13 only)""".stripMargin
41+
"""|A Scala compiler option is required to use RemoveUnused. To fix this problem,
42+
|update your build to add -Ywarn-unused (2.12.x), -Wunused (2.13.x), or
43+
|-Wunused:all (>=3.3.0)""".stripMargin
4944
)
5045
} else {
5146
config.conf

scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnusedConfig.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ case class RemoveUnusedConfig(
1212
privates: Boolean = true,
1313
@Description("Remove unused local definitions")
1414
locals: Boolean = true,
15-
@Description(
16-
"Remove unused pattern match variables (compatible with Scala 2.12 and 2.13)"
17-
)
15+
@Description("Remove unused pattern match variables")
1816
patternvars: Boolean = true,
19-
@Description(
20-
"Remove unused function parameters (compatible with Scala 2.12 and 2.13)"
21-
)
17+
@Description("Remove unused function parameters")
2218
params: Boolean = true
2319
)
2420

scalafix-tests/input/src/main/scala-2.12/test/removeUnused/RemoveUnusedPatternVars.scala

Lines changed: 0 additions & 75 deletions
This file was deleted.

scalafix-tests/input/src/main/scala-2.13/test/removeUnused/RemoveUnusedParams.scala

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)