scala / bug

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

`Serializable` limits the helpfulness of `-Xlint:infer-any` #12784

Open baffles opened 1 year ago

baffles commented 1 year ago

Reproduction steps

Scala version: 2.13.9

scala> sealed trait T; class C extends T
trait T
class C

scala> List.empty[T].contains("foo") // good: warns
                     ^
       warning: a type was inferred to be `Object`; this may indicate a programming error.
val res0: Boolean = false

scala> List.empty[C].contains("foo") // good: warns
                     ^
       warning: a type was inferred to be `Object`; this may indicate a programming error.
val res1: Boolean = false

scala> sealed trait T2; case class C2(foo: Int) extends T2
trait T2
class C2

scala> List.empty[T2].contains("foo") // good: warns
                      ^
       warning: a type was inferred to be `Object`; this may indicate a programming error.
val res2: Boolean = false

scala> List.empty[C2].contains("foo") // BAD: does NOT warn
val res3: Boolean = false

Problem

-Xlint:infer-any isn't triggering; seemingly because scalac is inferring Serializable in the no-warning case above. Seems like Serializable (and possibly some other things, like Product) should be warned on as well, or there should be another linter added for inferences to those general types. Otherwise, you miss potential mistakes when these other generic base types can be inferred.

som-snytt commented 1 year ago

Previous art

https://github.com/scala/scala/pull/5990 https://github.com/scala/scala/pull/6106

Let’s come back to this after 2.12.4