Skip to content

Unused warning occurred even if the variable was used #11175

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
tkrs opened this issue Sep 28, 2018 · 12 comments
Closed

Unused warning occurred even if the variable was used #11175

tkrs opened this issue Sep 28, 2018 · 12 comments

Comments

@tkrs
Copy link

tkrs commented Sep 28, 2018

Given the following code:

object Hello {
  for {
    a <- Option(1)
    b <- Option(2) if a == 1
  } yield a + b
}

Compiling with -Ywarn-unused:params on 2.12.7 gives the following warning:

[warn] /Users/tkrs/code/github.com/tkrs/hello/src/main/scala/Hello.scala:7:5: parameter value b in value$anonfun is never used
[warn]     b <- Option(2) if a == 1
[warn]     ^
[warn] one warning found

However, no warning occurred on 2.12.6.

@SethTisue
Copy link
Member

/cc @som-snytt

@som-snytt
Copy link

Thanks, I'm already taking a look, sigh. Someone brought up another issue, not sure if it's a ticket, but my response was that the current approach will remain brittle, short of a rewrite.

@som-snytt
Copy link

It snags the filtering expression:

Replaying: for (x <- Option(1); y <- Option(2) if x > 1) yield x + y
[[syntax trees at end of                    parser]] // <console>
        val res2 = Option(1).flatMap(((x) => Option(2).withFilter(((y) => x.$greater(1))).map(((y) => x.$plus(y)))))

The related umbrella issue #10287 is still open.

@adriaanm
Copy link
Contributor

Ok, I'm rescheduling for 2.13.x, since we shouldn't be rewriting things in 2.12 anymore, and it sounds like that's what's required for a robust fix.

@manuchadha1979
Copy link

Hi

In which release this issue has been or is planned to be resolved?

regards

@dwijnand
Copy link
Member

@manuchadha1979 this is currently targeting the 2.13.1 milestone, but it looks like no one has picked up fixing it yet.

@manuchadha1979
Copy link

Thanks. Would the current work around be either not to use the compiler option or not to use use the for comprehension?

@dwijnand
Copy link
Member

It's your choice.

An alternative to not using the compiler option or not using the for comprehension is this:

 object Hello {
   for {
     a <- Option(1)
     b <- Option(2) if a == 1
+    useB = b // WORKAROUND scala/bug#11175 -Ywarn-unused:params false positive
   } yield a + b
 }

@som-snytt
Copy link

Modulo side-effects:

$ scala -Wunused:params

     ________ ___   / /  ___
    / __/ __// _ | / /  / _ |
  __\ \/ /__/ __ |/ /__/ __ |
 /____/\___/_/ |_/____/_/ | |
                          |/  version 2.13.0

scala> for (x <- Option(1); y <- Option(2) if x > 1) yield x + y
                            ^
       warning: parameter value y in anonymous function is never used
res0: Option[Int] = None

scala> for (x <- Option(1) if x > 1; y <- Option(2)) yield x + y
res1: Option[Int] = None

@joan38
Copy link

joan38 commented Oct 23, 2019

It's your choice.

An alternative to not using the compiler option or not using the for comprehension is this:

 object Hello {
   for {
     a <- Option(1)
     b <- Option(2) if a == 1
+    useB = b // WORKAROUND scala/bug#11175 -Ywarn-unused:params false positive
   } yield a + b
 }

I used a _ instead:

_ = b // Workaround scala/bug#11175 -Ywarn-unused:params false positive

@som-snytt
Copy link

Apologies to the 22 thumbs-up voters, who probably meant thumbs-down.

The technology was pushed to its limit.

Is it a coincidence that it topped out at 22, or does github use a Tuple?

@SethTisue SethTisue modified the milestones: 2.13.2, 2.13.3 Feb 6, 2020
@SethTisue SethTisue modified the milestones: 2.13.3, 2.13.4 May 12, 2020
@SethTisue
Copy link
Member

consolidating at #10287

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

9 participants