-
-
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
Add new Lint/HashCompareByIdentity
cop
#8796
Conversation
ac0da48
to
420fe8b
Compare
Thank you for the implementation. I was also considering this style rule implementation. Cc @rubocop-hq/rubocop-core |
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.
I ❤️ it when a cop improves our own code base 🎉
Even though both cases deal with comparing by identity, I wonder if we shouldn't split this into two cops:
- Some people might not like
compare_by_identity
and want to disable that (but not the use ofequal?
- The
==
case is safe (I think 😅), the second case is not quite safe: although unlikely, the hash could store object ids and other stuff that need be compared by values.
Maybe @bbatsov has an opinion on this
Edit: Just read @koic's remark, glad to see we agree about splitting this 😄
return unless receiver && argument | ||
def_node_matcher :id_comparison?, <<~PATTERN | ||
(send | ||
(send $!nil? :object_id) :== |
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.
Why the !nil?
? We should still replace object_id == other.object_id
with equal?(other)
, and other.object_id == object_id
with other.equal?(self)
no?
|
||
replacement = "#{receiver.source}.equal?(#{argument.source})" | ||
def_node_matcher :id_as_hash_key?, <<~PATTERN | ||
(send _ {:key? :has_key? :fetch :[] :[]=} (send !nil? :object_id) ...) |
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.
Same here: hash[object_id]
should be an offense
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.
I ❤️ it when a cop improves our own code base 🎉
Even though both cases deal with comparing by identity, I wonder if we shouldn't split this into two cops:
- Some people might not like
compare_by_identity
and want to disable that (but not the use ofequal?
- The
==
case is safe (I think 😅), the second case is not quite safe: although unlikely, the hash could store object ids and other stuff that need be compared by values.
d0eeb56
to
2c456dd
Compare
Extracted into a separate cop. |
Lint/IdentityComparison
cop with a rule for object_id
as hash keysLint/CompareByIdentity
cop
2c456dd
to
074ce48
Compare
Lint/CompareByIdentity
copLint/HashCompareByIdentity
cop
Thanks! |
This implements a second part of https://rubystyle.guide/#identity-comparison