Skip to content

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

Open
@som-snytt

Description

@som-snytt

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 scala/scala#10466 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions