6

According to this it is possible to suppress any warnings of unused parameters.

However, I think suppressing all the cases can actually hide bugs, so I want a more granular way for suppressing those.

There is a definite use case, where this warning in most cases is meaningless, I will explain it via an example.

For example the following hypothetical piece of code of a proxy:

trait HeadersGenerator {
  def generateHeaders(request: Request): Seq[(String, String)]
}

class HeaderMutator extends HeadersGenerator {
  override def generateHeaders(request: Request): Seq[(String, String)] = {
    val oldValue = request.getHeader("SomeHeader")    

    Seq("SomeHeader" -> s"$oldValue - new")
  }
}

class ConstantHeaderGenerator extends HeadersGenerator {
  // here the 'request' parameter is not in use, and needed to be suppressed - 
  // it is not interesting that we do not use it here, since we 
  // don't have anything to do with that because it is inherited
  override def generateHeaders(request: Request): Seq[(String, String)] = {
    Seq("GeneratedHeader" -> "something")
  }

  // here the 'unused' parameter is interesting, and I would like to see a warning
  private def shouldWarn(unused: String) {
    // the parameter is not un use here
  }
}

// somewhere in code
val headers = Seq(
  new HeadersMutator,
  new ConstantHeaderGenerator
).flatMap(_.generateHeaders(request))
Genry
  • 1,358
  • 2
  • 23
  • 39
  • 1
    You can use [silencer](https://github.com/ghik/silencer) – ghik Aug 15 '19 at 18:10
  • I haven't checked but I have a feeling that this has been fixed. If the method is an override then I'm pretty sure it doesn't generate a warning if the parameters are unused. – steinybot Mar 29 '23 at 22:37

3 Answers3

3

Use this annotation for compiler:

  private def shouldWarn(@nowarn unused: String) {
    // the parameter is not in use here
  }
Julia
  • 161
  • 2
  • 3
  • 12
1

I think if you use _ instead of a parameter name, in your case request, the warning will go away. You don't have to name parameters if you don't use them.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
nathan g
  • 858
  • 9
  • 17
0

One can always use one's parameters in a way that does nothing. For example:

object CombinatoryLogic {
    def unused[T](arg: T): Unit = arg match { case _ => () }

    // Church encoding of true
    def k[A, B](consequent: A, alternative: B): A = {
        unused(alternative)
        consequent
    }
}

On my scale it's a moderate kludge. On the benefit side, it's always available.

Jonas Kölker
  • 7,680
  • 3
  • 44
  • 51