Skip to content

Commit 4f4cbee

Browse files
committed
RemoveUnused: pattern vars updates post 3.7.0
- document deprecation of `-Wunused:unsafe-warn-patvars` - document fragmented canonical patvars behavior
1 parent 6d90e9b commit 4f4cbee

File tree

12 files changed

+1607
-14
lines changed

12 files changed

+1607
-14
lines changed

docs/rules/RemoveUnused.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ case class AB(a: Int, b: String)
7272
object Main {
7373
val example = AB(42, "lol")
7474
example match {
75-
case AB(a, b) => println("Not used")
75+
case AB(foo, bar) => println("Not used")
76+
}
77+
example match {
78+
case AB(a, b) => println("Not used, but canonical names")
7679
}
7780
}
7881
// after
@@ -81,14 +84,14 @@ object Main {
8184
example match {
8285
case AB(_, _) => println("Not used")
8386
}
87+
example match {
88+
case AB(a, b) => println("Not used, but canonical names")
89+
}
8490
}
8591
```
8692

87-
On Scala 3, `-Wunused:unsafe-warn-patvars` is required.
88-
89-
On Scala 2.13.15+, canonical patterns (vars with the same names as the
90-
attributes) do not trigger unused warnings, so the input above will not
91-
be rewritten. See https://github.com/scala/bug/issues/13035.
93+
On Scala 3, an additional `-Wunused:unsafe-warn-patvars` option is required
94+
until Scala 3.7.0.
9295

9396
### Remove unused function parameters
9497

project/Dependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sbt._
66

77
object Dependencies {
88
val scala212 = sys.props.getOrElse("scala212.nightly", "2.12.20")
9-
val scala213 = sys.props.getOrElse("scala213.nightly", "2.13.16")
9+
val scala213 = sys.props.getOrElse("scala213.nightly", "2.13.16") // remove 2.13.14 hack in RuleSuite when bumping
1010
val scala33 = "3.3.5"
1111
val scala35 = "3.5.2"
1212
val scala36 = "3.6.4"

project/ScalafixBuild.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
8484
scalaVersion.value.startsWith("2.12")
8585
}
8686
lazy val warnUnused = Def.setting {
87-
if (isScala3.value) Seq("-Wunused:all", "-Wunused:unsafe-warn-patvars")
87+
if (isScala3.value)
88+
Seq(
89+
"-Wunused:all",
90+
"-Wunused:unsafe-warn-patvars" // only needed for <3.7.0
91+
)
8892
else if (isScala213.value) Seq("-Wunused")
8993
else Seq("-Ywarn-unused")
9094
}

scalafix-tests/expect/src/test/scala/scalafix/tests/rule/RuleSuite.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,23 @@ class RuleSuite extends AbstractSemanticRuleSuite with AnyFunSuiteLike {
3636
override def runOn(diffTest: RuleTest): Unit = {
3737
def stripPatch(v: String) = v.split('.').take(2).mkString(".")
3838

39+
val inputSV = props.scalaVersion
40+
val path = diffTest.path.input.toNIO.toString
41+
3942
val versionMismatch =
40-
stripPatch(RulesBuildInfo.scalaVersion) != stripPatch(props.scalaVersion)
43+
stripPatch(RulesBuildInfo.scalaVersion) != stripPatch(inputSV)
4144
val explicitResultTypesTest =
42-
diffTest.path.input.toNIO.toString.contains(
45+
path.contains(
4346
"explicitResultTypes" + java.io.File.separator // don't skip tests with a suffix
4447
)
4548

46-
// ExplicitResultTypes can only run against sources compiled with the sam
47-
// binary version as the one used to compile the rule
48-
if (versionMismatch && explicitResultTypesTest) return
49+
if (
50+
// ExplicitResultTypes can only run against sources compiled with the same
51+
// binary version as the one used to compile the rule
52+
(versionMismatch && explicitResultTypesTest) ||
53+
// RemoveUnusedPatternVars has the old 2.12.x behavior for 2.13.14
54+
(inputSV == "2.13.14" && path.endsWith("RemoveUnusedPatternVars.scala"))
55+
) return
4956
else super.runOn(diffTest)
5057
}
5158

scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedPatternVars.scala renamed to scalafix-tests/input/src/main/scala-2/test/removeUnused/RemoveUnusedPatternVars.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ object ob {
1212
// Add code that needs fixing here.
1313
val example = AB(42, "lol")
1414

15+
example match {
16+
case AB(aa, bb) => println("https://github.com/scala/bug/issues/13035")
17+
}
1518
example match {
1619
case AB(_, _) => println("Not used, good")
1720
}

0 commit comments

Comments
 (0)