Skip to content

Commit 900c14a

Browse files
committed
RemoveUnused.params on Scala 3 Next
scala/scala3#20894
1 parent 0098056 commit 900c14a

File tree

7 files changed

+35
-14
lines changed

7 files changed

+35
-14
lines changed

docs/rules/RemoveUnused.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ On Scala 2.13.15+, canonical patterns (vars with the same names as the
9090
attributes) do not trigger unused warnings, so the input above will not
9191
be rewritten. See https://github.com/scala/bug/issues/13035.
9292

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

9595
```scala
9696
// before
@@ -103,6 +103,8 @@ object Main {
103103
}
104104
```
105105

106+
On Scala 3, this is only supported for 3.7.0+.
107+
106108
## Formatting
107109

108110
> This rule does a best-effort at preserving original formatting. In some cases,
@@ -176,8 +178,7 @@ $ scala3 -W
176178
- nowarn,
177179
- all,
178180
- imports :
179-
Warn if an import selector is not referenced.
180-
NOTE : overrided by -Wunused:strict-no-implicit-warn,
181+
Warn if an import selector is not referenced.,
181182
- privates :
182183
Warn if a private member is unused,
183184
- locals :
@@ -188,13 +189,13 @@ $ scala3 -W
188189
Warn if an implicit parameter is unused,
189190
- params :
190191
Enable -Wunused:explicits,implicits,
192+
- patvars :
193+
Warn if a variable bound in a pattern is unused,
191194
- linted :
192195
Enable -Wunused:imports,privates,locals,implicits,
193196
- strict-no-implicit-warn :
194197
Same as -Wunused:import, only for imports of explicit named members.
195-
NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all,
198+
NOTE : This overrides -Wunused:imports and NOT set by -Wunused:all,
196199
- 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.
200+
Deprecated alias for `patvars`
200201
```

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

+5-2
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-rules/src/main/scala/scalafix/internal/rule/RemoveUnusedConfig.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ case class RemoveUnusedConfig(
1414
locals: Boolean = true,
1515
@Description("Remove unused pattern match variables")
1616
patternvars: Boolean = true,
17-
@Description("Remove unused function parameters (Scala 2 only)")
17+
@Description("Remove unused function parameters")
1818
params: Boolean = true
1919
)
2020

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

-2
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")
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

-2
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")
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)