scala / bug

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

-Xlint:infer-any should warn if Any is inferred for if-else and method result #12829

Open som-snytt opened 11 months ago

som-snytt commented 11 months ago

Reproduction steps

Scala version: 2.13.11

scala> List(42, "")
       ^
       warning: a type was inferred to be `Any`; this may indicate a programming error.
val res0: List[Any] = List(42, "")

scala> val b = collection.mutable.ListBuffer.empty[Int]
val b: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

scala> def f(i: Int, t: Boolean) = if (t) b += i
def f(i: Int, t: Boolean): Any

Problem

It inferred Any, so it should tell me so. It may indicate a programming error!

In this case, b.+= is not Unit. The RHS and the result of f is inferred Any.

Noticed at https://github.com/scala/scala/pull/10466#issuecomment-1636059262