Skip to content

Commit 66bff46

Browse files
committed
RemoveUnused.params on Scala 3 Next
scala/scala3#20894
1 parent 9303d7d commit 66bff46

File tree

7 files changed

+35
-16
lines changed

7 files changed

+35
-16
lines changed

docs/rules/RemoveUnused.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ object Main {
8484
}
8585
```
8686

87-
On Scala 3, `-Wunused:unsafe-warn-patvars` is required.
88-
8987
On Scala 2.13.15+, canonical patterns (vars with the same names as the
9088
attributes) do not trigger unused warnings, so the input above will not
9189
be rewritten. See https://github.com/scala/bug/issues/13035.
9290

93-
### Remove unused function parameters (Scala 2 only)
91+
### Remove unused function parameters
9492

9593
```scala
9694
// before
@@ -103,6 +101,8 @@ object Main {
103101
}
104102
```
105103

104+
On Scala 3, sources must be compiled with 3.7.0+.
105+
106106
## Formatting
107107

108108
> This rule does a best-effort at preserving original formatting. In some cases,
@@ -176,8 +176,7 @@ $ scala3 -W
176176
- nowarn,
177177
- all,
178178
- imports :
179-
Warn if an import selector is not referenced.
180-
NOTE : overrided by -Wunused:strict-no-implicit-warn,
179+
Warn if an import selector is not referenced.,
181180
- privates :
182181
Warn if a private member is unused,
183182
- locals :
@@ -188,13 +187,13 @@ $ scala3 -W
188187
Warn if an implicit parameter is unused,
189188
- params :
190189
Enable -Wunused:explicits,implicits,
190+
- patvars :
191+
Warn if a variable bound in a pattern is unused,
191192
- linted :
192193
Enable -Wunused:imports,privates,locals,implicits,
193194
- strict-no-implicit-warn :
194195
Same as -Wunused:import, only for imports of explicit named members.
195-
NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all,
196+
NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all,
196197
- unsafe-warn-patvars :
197-
(UNSAFE) Warn if a variable bound in a pattern is unused.
198-
This warning can generate false positive, as warning cannot be
199-
suppressed yet.
198+
Deprecated alias for `patvars`
200199
```

project/ScalafixBuild.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
8888
scalaVersion.value.startsWith("2.12")
8989
}
9090
lazy val warnUnused = Def.setting {
91-
if (isScala3.value) Seq("-Wunused:all", "-Wunused:unsafe-warn-patvars")
91+
if (isScala3.value) Seq("-Wunused:all")
9292
else if (isScala213.value) Seq("-Wunused")
9393
else Seq("-Ywarn-unused")
9494
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ class RemoveUnused(config: RemoveUnusedConfig)
9696
isUnusedPattern += diagnostic.position
9797
} else if (
9898
config.params &&
99-
(msg.startsWith("parameter") && msg.endsWith("is never used"))
99+
(
100+
msg.startsWith("parameter") && msg.endsWith("is never used") ||
101+
msg == "unused explicit parameter"
102+
)
100103
) {
101104
isUnusedParam += diagnostic.position
102105
}
@@ -189,7 +192,7 @@ class RemoveUnused(config: RemoveUnusedConfig)
189192
case Term.Function(params, _) =>
190193
params.collect {
191194
case param @ Term.Param(_, name, _, _)
192-
if isUnusedParam(param.pos) =>
195+
if isUnusedParam(param.pos) || isUnusedParam(name.pos) =>
193196
Patch.replaceTree(name, "_")
194197
}.asPatch
195198
}.asPatch

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ rule = RemoveUnused
33
*/
44
package test.removeUnused
55

6-
// Not available as of Scala 3.4.1
7-
// https://github.com/scalacenter/scalafix/issues/1937
86
object UnusedParams {
97
val f: String => Unit = unused => println("f")
108
val ff = (unused: String) => println("f")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
rule = RemoveUnused
3+
*/
4+
package test.removeUnused
5+
6+
object UnusedParams {
7+
val f: String => Unit = unused => println("f")
8+
val ff = (unused: String) => println("f")
9+
val fs = (used: String, unused: Long) => println(used)
10+
def g(x: String => Unit): Unit = ???
11+
g{implicit string => println("g")}
12+
}

scalafix-tests/output/src/main/scala-2/test/removeUnused/RemoveUnusedParams.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package test.removeUnused
22

3-
// Not available as of Scala 3.4.1
4-
// https://github.com/scalacenter/scalafix/issues/1937
53
object UnusedParams {
64
val f: String => Unit = _ => println("f")
75
val ff = (_: String) => println("f")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package test.removeUnused
2+
3+
object UnusedParams {
4+
val f: String => Unit = _ => println("f")
5+
val ff = (_: String) => println("f")
6+
val fs = (used: String, _: Long) => println(used)
7+
def g(x: String => Unit): Unit = ???
8+
g{implicit string => println("g")}
9+
}

0 commit comments

Comments
 (0)