HairyFotr / linter

Static Analysis Compiler Plugin for Scala
Apache License 2.0
268 stars 34 forks source link

ContainsTypeMismatch false positive when subtype relationship is inverted #54

Open vdichev opened 7 years ago

vdichev commented 7 years ago

To demonstrate the problem:

scala> class A; class B extends A defined class A defined class B

scala> val b = new B; val a: A = b b: B = B@3358f363 a: A = B@3358f363

scala> List(a).contains(b) res9: Boolean = true

scala> List(b).contains(a)

:16: warning: [ContainsTypeMismatch] List[B].contains(A) will probably return false, since the collection and target element are of unrelated types. List(b).contains(a) ^ res10: Boolean = true

The warning shouldn't be raised because it's possible for a List of B to contain an element of type A which is a supertype (not subtype) of B- it's obvious that it does return true.