Skip to content

[2.13.15] False negatives for unused pattern vars #13035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bjaglin opened this issue Sep 23, 2024 · 3 comments
Closed

[2.13.15] False negatives for unused pattern vars #13035

bjaglin opened this issue Sep 23, 2024 · 3 comments

Comments

@bjaglin
Copy link

bjaglin commented Sep 23, 2024

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]             ^^^^^^
$
bjaglin added a commit to bjaglin/scalafix that referenced this issue Sep 23, 2024
bjaglin added a commit to bjaglin/scalafix that referenced this issue Sep 23, 2024
@som-snytt
Copy link

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.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Sep 23, 2024
@bjaglin
Copy link
Author

bjaglin commented 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
Copy link
Author

bjaglin commented Apr 12, 2025

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.

FTR, it's now on 3.7.0 via scala/scala3#20894

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants