5

I have this little scala example:

object Test {
  def add(x: Int, y: Int) = {
    val z = x - y
    x match {
      case 0 => 0 - y
      case 1 => 1 - y
      case _ => x - y
    }
    x + y
  }

  def main(args: Array[String]) {
    println(add(5, 6))
  }
}

I feel that scala should warn about 'z' and the 'x match ...' being unused. I did not notice any compiler options to turn on more warnings. I'm using scala 2.10.1.

Thoughts? Thanks!

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Fysx
  • 805
  • 1
  • 6
  • 12
  • 3
    This is perfectly valid scala code as indicated by the compiler. Unused variable warnings are typically something that a source code analyzer would find and warn about. For a similar piece of code the `java` compiler does not show a warning either. – Brian Jul 17 '13 at 05:14
  • @Brian I'm not sure javac is the benchmark. The 3 upvoters say, I don't need no stinkin' type safety! I cut scalac some slack on the match, it's still trying to nail down exhaustiveness. It can warn on discarding values for Unit, which suggests it would warn if it could warn. – som-snytt Jul 17 '13 at 10:15
  • Try adding the `"-Ywarn-value-discard"` compiler option. – Brian McKenna Feb 25 '14 at 23:54

1 Answers1

8

As you can see here, "unused" warnings will be introduced in the next version of scala, 2.11.

Warn about unused private / local terms and types, and unused imports, under -Xlint

You can try them using the last milestone.

Giovanni Caporaletti
  • 5,426
  • 2
  • 26
  • 39
  • In 2.11: `-Ywarn-unused Warn when local and private vals, vars, defs, and types are unused.` – nedim Oct 15 '15 at 12:49