-
Notifications
You must be signed in to change notification settings - Fork 21
Missing warning for insensible comparison #6593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6593?orig=1 |
Lee Mighdoll (mighdoll) said (edited on May 2, 2013 3:27:53 AM UTC): $ ./scala
Welcome to Scala version 2.11.0-20130430-161342-13cf0481d2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_11).
scala> List("bah") == 1
res1: Boolean = false (warns nicely if reversed) scala> 1 == List("foo")
<console>:8: warning: comparing values of types Int and List[String] using `==' will always yield false
1 == List("foo")
^
res0: Boolean = false Realizing that it's hard to know for certain if someone has overridden equals, I suppose this is tricky. Maybe an annotation on some library equals methods: @neverEqualsPrimitive. Not clear to me how often this comes up or whether there's anything more reasonable to be done. |
@som-snytt said: $ scala -Xlint
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def f(c: Char) = c == "o"
<console>:10: warning: comparing values of types Char and String using `==' will always yield false
def f(c: Char) = c == "o"
^
f: (c: Char)Boolean
scala> def f(s: String) = s.exists(_ == "o")
f: (s: String)Boolean
scala> def f(s: String) = s.exists((c: Char) => c == "o")
f: (s: String)Boolean |
Ivan Jager (aij) said (edited on Jun 20, 2016 1:18:10 PM UTC): scala> val f = (c: Char) => c == "ooo"
f: Char => Boolean = <function1>
scala> val f = 1 == (_:Char)
f: Char => Boolean = <function1>
scala> val f = "ooo" == (_:Char)
f: Char => Boolean = <function1>
scala> val f = (_:Char) == "ooo"
f: Char => Boolean = <function1>
scala> val f: Char => Boolean = _ == "ooo"
f: Char => Boolean = <function1> Maybe it's a matter of methods vs. functions? |
Note some expected warnings are emitted now:
Not the ones scared off by |
@som-snytt what do you think about assuming symmetry in Also we could hard code assumptions about |
@lrytz it's not that simple. I took a look at what we have. A "warnable equals" is (based on the receiver type):
The current comparison is "warnable" if it uses either
There are two warning types
The "non-sensible" warning (
Generally, the "non-sensible" warning is not an approximation, when it shows up it's correct. However not in this case: scala> case class C(x: Int)
scala> class CC extends C(1) { override def equals(o: Any) = true }
scala> (new CC: C) == 42
^
warning: comparing values of types C and Int using `==` will always yield false
val res0: Boolean = true We could remove the warning or tone down its confidence for case-equals that are not final. The "unrelated" warning is issued
That warning can be incorrect: scala> class C
scala> class D extends C { override def equals(o: Any) = true }
scala> class E // unrelated
scala> val c = new C; val d = new D; val e = new E
scala> d == e
val res1: Boolean = true
scala> (d: C) == e
^
warning: C and E are unrelated: they will most likely never compare equal
val res2: Boolean = true Now the question is, how can we argue for warning about It's not a "warnable equals", the method is overridden and we know nothing about its implementation. So we should probably not issue a "non-sensible" warning (which is supposed to be correct). I see two options:
scala> class C { override def equals(o: Any) = super.equals(o) }
| class D
| val c = new C
| val d = new D
scala> d == c
^
warning: D and C are unrelated: they will most likely never compare equal
val res0: Boolean = false
scala> c == d
val res1: Boolean = false There are cases where only 1) or only 2) would trigger a warning. For example, with scala/scala#10850, now |
Something is up with these; I swear some of them used to warn. Really, I can't spot "Nil == 0" ?
The text was updated successfully, but these errors were encountered: