Skip to content
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

-Xlint:unused false positive with self type #10314

Closed
Sciss opened this issue May 14, 2017 · 4 comments · Fixed by scala/scala#10305
Closed

-Xlint:unused false positive with self type #10314

Sciss opened this issue May 14, 2017 · 4 comments · Fixed by scala/scala#10305
Assignees
Labels
Milestone

Comments

@Sciss
Copy link

Sciss commented May 14, 2017

(related: #10313)

E.g.

trait Tx

trait Base {
  def foo(implicit tx: Tx): Unit
}

trait MixIn {
  self: Base =>

  def foo(implicit tx: Tx): Unit = println("Hello")
}

Obviously this shouldn't produce a warning.

@som-snytt
Copy link

I don't think the linked ticket is relevant.

The idea here is that trait MixIn extends Base doesn't warn, so the self-type version shouldn't warn either.

The warning is:

<console>:20: warning: parameter value tx in method foo is never used
         def foo(implicit tx: Tx): Unit = println("Hello")

@SethTisue
Copy link
Member

I'm failing to see how this is a bug? tx isn't used.

@SethTisue SethTisue closed this as not planned Won't fix, can't repro, duplicate, stale Feb 10, 2023
@som-snytt
Copy link

som-snytt commented Feb 10, 2023

Ah, he said after a moment, because the parameter is required by an inherited signature, we don't want to warn.

➜  ~ scala -Wunused
Welcome to Scala 2.13.10 (OpenJDK 64-Bit Server VM, Java 19).
Type in expressions for evaluation. Or try :help.

scala> def f(i: Int) = println("hi")
             ^
       warning: parameter value i in method f is never used
def f(i: Int): Unit

scala> trait Tx
trait Tx

scala> trait T { def f(implicit tx: Tx): Unit }
trait T

scala> class C extends T { def f(implicit tx: Tx) = println("hi") }
class C

scala> trait U { _: T => def f(implicit tx: Tx) = println("hi") }
                                        ^
       warning: parameter value tx in method f is never used
trait U

scala>

I've forgotten the knobs

➜  ~ scala -Wunused:help
Enable or disable specific `unused` warnings
  imports     Warn if an import selector is not referenced.
  patvars     Warn if a variable bound in a pattern is unused.
  privates    Warn if a private member is unused.
  locals      Warn if a local definition is unused.
  explicits   Warn if an explicit parameter is unused.
  implicits   Warn if an implicit parameter is unused.
  synthetics  Warn if a synthetic implicit parameter (context bound) is unused.
  nowarn      Warn if a @nowarn annotation does not suppress any warnings.
  params      Enable -Wunused:explicits,implicits,synthetics.
  linted      -Xlint:unused.
Default: All choices are enabled by default.

Edit: you can also write override for method from self-type (versus extends).

scala> trait U { _: T => override def f(implicit tx: Tx) = println("hi") }

Edit: it would be helpful to have a strict mode that bypasses the bypasses.
https://discord.com/channels/632277896739946517/632277897448652844/1073552811750592533
where someone says "I deleted the usage of the param and it didn't warn so I thought it was ok." The heuristics kick in for obvious RHS such as def f(i: Int) = 42 or = ???.

I wonder what Murderbot would say in this moment. https://en.wikipedia.org/wiki/The_Murderbot_Diaries

@som-snytt som-snytt reopened this Feb 10, 2023
@som-snytt
Copy link

Desired:

➜  ~ skala -Wunused
Welcome to Scala 2.13.11-20230210-163944-806042b (OpenJDK 64-Bit Server VM, Java 19).
Type in expressions for evaluation. Or try :help.

scala> trait Tx
trait Tx

scala> trait T { def f(implicit tx: Tx): Unit }
trait T

scala> trait U { _: T => override def f(implicit tx: Tx) = println(tx) }
trait U

@SethTisue SethTisue added this to the Backlog milestone Feb 11, 2023
@SethTisue SethTisue added the lint label Feb 11, 2023
@SethTisue SethTisue modified the milestones: Backlog, 2.13.12 Jul 6, 2023
@SethTisue SethTisue changed the title -Xlint:unused false positive with self type, in Scala 2.12.2 -Xlint:unused false positive with self type Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants