-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
SI-8939 warn about non-sensible equals in anonymous functions #6486
SI-8939 warn about non-sensible equals in anonymous functions #6486
Conversation
Possibly this is too ham-fisted for I've tried to help tweak these warnings, and options are limited for assuming too much. A third-party linter could lint things like comparing -- Wait, isn't scalafix a tool for fixing syntax? |
Yes, this is pretty harsh indeed: Welcome to Scala 2.13.0-20180402-083257-6f4848e (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_161).
Type in expressions for evaluation. Or try :help.
scala> final case class UserId(value: Int) ; final case class User(id: Option[UserId], other: Int)
defined class UserId
defined class User
scala> val l: List[User] = List(User(Some(UserId(1)), 2))
l: List[User] = List(User(Some(UserId(1)),2))
scala> l.find(_.id == Some(UserId(1)))
^
warning: Option[UserId] and Some[UserId] are unrelated: they will most likely never compare equal
res0: Option[User] = Some(User(Some(UserId(1)),2)) It's also got a lenient side: scala> l.find(_.id == Option(1))
res1: Option[User] = None |
* Use widened type * Use =:= comparison * Add more tests
Thanks for chiming in; I'm totally fine with making something like this
Good eye and nice testing :) I've gone and updated this PR so the second scenario will get caught. As for the first (comparing That said, in the last commit, I've allowed upwards class hierarchy search, which I think handles comparing |
* Allow upwards class hierachy search but check for common class not object
aea33d9
to
4ba62cc
Compare
There are issues issued for other comparisons. Wow, for instance, It's like on the Mission Impossible tv series where the tape promises to disavow all knowledge of your actions if you are caught or killed. String? Who's that? I can't speak authoritatively, but I suspect they might want to limit the depth of the rabbit hole for this linter check, and prefer to externalize it. They might like the Edit: but |
Weird, I just added another check and it shows up:
EDIT: ah you meant by default, without the lint added in this PR? |
@dwijnand perhaps you'd like to take a look? |
val warnNumericWiden = BooleanSetting("-Ywarn-numeric-widen", "Warn when numerics are widened.") | ||
val warnDeadCode = BooleanSetting("-Ywarn-dead-code", "Warn when dead code is identified.") | ||
val warnValueDiscard = BooleanSetting("-Ywarn-value-discard", "Warn when non-Unit expression results are unused.") | ||
val warnNumericWiden = BooleanSetting("-Ywarn-numeric-widen", "Warn when numerics are widened.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please treat all boolean settings with respect. There's no need to shove them to the right. 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole world is moving to the right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beyoncé isn't, she prefers
To the left, to the left
Everything you own in the box to the left
My personal stance is that we should avoid putting more linting in the compiler and instead implement them in Scalafix where they can (1) grow, multiply and evolve outside of the release cadence of Scala, and (2) they have a chance to be rewrite rules (as opposed to just warning/notifying rules). (Also expressed some of these thoughts in scala/scala-dev#430 (comment)) As such my vote is for ❌ rejecting this PR (which isn't a GitHub review option..) |
I agree this breaks too easily for it to be included in the compiler. IMO warnings should be implemented in the compiler if there are no (few) false positives, and it's difficult to implement the same in a lint tool. As an example, in the collections, types that are not subtypes of each other can be equal
|
Adds a new
XlintYwarn that warns on comparison of mismatched types.I understand that this is a bit of a ham-fisted approach, but something like this, in the vein of warn-me-if-i-am-comparing-any-unequal-types, seems to be something that (a) people want but doesn't exist and (b) would solve a lot of issues when combined with fatal warnings.
Closes scala/bug#8939 (https://issues.scala-lang.org/browse/SI-8939)