Skip to content

For-comprehension with Option - incorrectly marked parameter as unused #12719

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
kgadek opened this issue Jan 26, 2023 · 2 comments
Closed

For-comprehension with Option - incorrectly marked parameter as unused #12719

kgadek opened this issue Jan 26, 2023 · 2 comments

Comments

@kgadek
Copy link

kgadek commented Jan 26, 2023

Reproduction steps

Scala version: 2.13.9 executed with -Ywarn-unused -Werror.

[info] Starting scala interpreter...
Welcome to Scala 2.13.9 (OpenJDK 64-Bit Server VM, Java 11.0.18).
Type in expressions for evaluation. Or try :help.

scala> for { a1 <- Some(1); a2 <- Some(0); if !(a1 > 10); res = a1 - a2 } yield res
                            ^
       warning: parameter value a2 in anonymous function is never used
error: No warnings can be incurred under -Werror.


scala> for { a2 <- Some(0); a1 <- Some(1); if !(a1 > 10); res = a1 - a2 } yield res
val res8: Option[Int] = Some(1)

Problem

This looks somewhat similar to #11343. The above statement should compile without any warning. Reordering unrelated statements fixes the error.

I've been able to reproduce it on scastie: https://scastie.scala-lang.org/fY4Fp3sAQ0unBDkf1uibdg — it seems 2.13.10 is also affected

@SethTisue SethTisue changed the title For-comprehension with Optional - incorrectly marked parameter as unused For-comprehension with Option - incorrectly marked parameter as unused Jan 26, 2023
@SethTisue
Copy link
Member

SethTisue commented Jan 26, 2023

Minimizes to for { a2 <- Some(0); if true } yield a2

-Vprint:parser shows us that the for desugars to Some(0).withFilter(a2 => true).map(a2 => a2), which of course gives the same warning.

The desugared form makes it clear why the warning is emitted; in withFilter(a2 => true), the a2 parameter is in fact not used.

@som-snytt this seems familiar — surely there's a ticket on this already?

@som-snytt
Copy link

yes, this is #10287

I will "consolidate" this example on that ticket.

Maybe I will take a swing at it when I take a last look at related warnings. Especially now that Dotty has unused warnings and we are motivated if not obligated to maintain parity.

That ticket is only 2500 tickets ago, but it feels like half a decade.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants