scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Avoid unused param warning in unused definition #12992

Closed som-snytt closed 4 months ago

som-snytt commented 5 months ago

Avoid warning if method is marked unused.

https://github.com/scala/scala/blob/v2.13.14/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala#L99

Avoid warning if parameter type is singleton.

https://github.com/scala/scala/blob/v2.13.14/src/compiler/scala/tools/nsc/ast/TreeDSL.scala#L132

Problem

Probably all unused warnings should be silenced by unused enclosing definition, much like deprecation.

som-snytt commented 4 months ago

What about

class `t12992 enclosing def is unused` {
  private val n = 42
  @annotation.unused def f() = n + 2 // unused code uses n
}

Is n unused? The PR doesn't look inside f.

som-snytt commented 4 months ago

I like that behavior but it's necessary to consider side effects.

In this example, max is untested but you must inspect the rhs for update of the var.

    var evaluatedCountOfMaxBy = 0

    val max = list.maxBy(x => {
      evaluatedCountOfMaxBy += 1
      x * 10
    })
som-snytt commented 4 months ago

not sure why I wrote this trick, but it is undone.

    type ignore = annotation.unused
    @ignore def ignore: ignore = ???  // ignore that ignore looks unused