scala does not warn about unused computation or value
Asked Answered
U

1

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?

Uniformize answered 17/7, 2013 at 5:2 Comment(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.Viridis
@Viridis 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.Insolence
Try adding the "-Ywarn-value-discard" compiler option.Primeval
K
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.

Khalilahkhalin answered 17/7, 2013 at 6:44 Comment(1)
In 2.11: -Ywarn-unused Warn when local and private vals, vars, defs, and types are unused.Rejoice

© 2022 - 2024 — McMap. All rights reserved.