You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My colleagues and I are porting a large codebase to Scala, and we're
finding we often use Option for fields or variables that are
"optional". (In the Python original of the codebase, these would be
variables where None is an acceptable value.) This results in a lot of
comparisons like
if (someUid == someOtherUid) ...
where it's easy to forget that, e.g., someUid is of type Option[Int]
while someOtherUid is of type Int. This comparison will always fail,
even in the case where the programmer intended it to succeed. I'm
therefore interested in compiler warnings that would help Scala
programmers avoid this kind of bug.
In nsc/typechecker/RefChecks.scala, we have a number of warnings along
these lines, but none of them quite manage to catch this case. There
is, however, this crude, effective, but disabled warning:
// Warning on types without a parental relationship. Uncovers a lot of
// bugs, but not always right to warn.
if (false) {
if (nullCount == 0 && possibleNumericCount < 2 &&
!(receiver isSubClass actual) && !(actual isSubClass receiver))
unrelatedTypes()
}
This warning is valuable enough, despite some false positives, that I
want to use it. I've attached a short patch which turns it on with a flag
-Ywarn-compare-unrelated and adds the flag to -Xlint.
I believe it should also be possible to warn on cases like Option[Int]
vs Int without any false positives, but it will take more work; I'll
open a separate ticket for that.
The text was updated successfully, but these errors were encountered:
[Patch attached.]
My colleagues and I are porting a large codebase to Scala, and we're
finding we often use Option for fields or variables that are
"optional". (In the Python original of the codebase, these would be
variables where None is an acceptable value.) This results in a lot of
comparisons like
if (someUid == someOtherUid) ...
where it's easy to forget that, e.g., someUid is of type Option[Int]
while someOtherUid is of type Int. This comparison will always fail,
even in the case where the programmer intended it to succeed. I'm
therefore interested in compiler warnings that would help Scala
programmers avoid this kind of bug.
In nsc/typechecker/RefChecks.scala, we have a number of warnings along
these lines, but none of them quite manage to catch this case. There
is, however, this crude, effective, but disabled warning:
This warning is valuable enough, despite some false positives, that I
want to use it. I've attached a short patch which turns it on with a flag
-Ywarn-compare-unrelated and adds the flag to -Xlint.
I believe it should also be possible to warn on cases like Option[Int]
vs Int without any false positives, but it will take more work; I'll
open a separate ticket for that.
The text was updated successfully, but these errors were encountered: