Not planned
Description
It seems that 2.13.15 no longer reports unused pattern vars when they have the same name as the attributes.
Reproduction steps
//> using scala 2.13.15
//> using options -Wunused
case class CC(field1: Int, field2: Int)
object CC {
CC(42, 42) match {
case CC(field1, field2) => 1
}
}
Problem
Actual
No warning
$ scala-cli compile PatternVars.scala
$
Expected
Warnings like in 2.13.14
$ scala-cli compile -S 2.13.14 PatternVars.scala
[warn] ./PatternVars.scala:8:21
[warn] pattern var field2 in value <local CC> is never used: use a wildcard `_` or suppress this warning with `field2@_`
[warn] case CC(field1, field2) => 1
[warn] ^^^^^^
[warn] ./PatternVars.scala:8:13
[warn] pattern var field1 in value <local CC> is never used: use a wildcard `_` or suppress this warning with `field1@_`
[warn] case CC(field1, field2) => 1
[warn] ^^^^^^
$
Activity
workaround scala/bug#13035
workaround scala/bug#13035
som-snytt commentedon Sep 23, 2024
Thanks for the report and for using
-Wunused
!This is a feature, never to warn on the "canonical pattern".
It's mentioned in the comment at scala/scala#10812
but I don't see where lrytz proposed it. Possibly, the idea was inherited from
-Xlint:pattern-shadow
.The experience in the scala/scala code base was that warning on
case Apply(fun, args)
for these lints was too noisy.The previous workaround was
case Apply(fun @ _, args @ _)
to mean that the var names are documentary, but that is too verbose to be useful. That was a workaround for lacking named args in patterns.So for warning, the idiom works like
case x: Apply => import x.*
if the import did not warn as unused.A
-strict
option was not proposed because there are too many knobs, and the goal was the happy medium that errs on the side of false negative. A 3rd party lint could provide fine-grained config.bjaglin commentedon Sep 23, 2024
Oh, thanks! Do you know by any chance if there is a plan to forward-port this change to Scala 3 for consistency? I guess not - I couldn't find any related ticket.
bjaglin commentedon Apr 12, 2025
FTR, it's now on 3.7.0 via scala/scala3#20894