Skip to content
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 Style/ClassEqualityComparison cop #8833

Merged
merged 1 commit into from Oct 8, 2020

Conversation

fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Oct 2, 2020

This cop enforces the use of Object#instance_of? instead of class comparison for equality.

# bad
var.class == Date
var.class.equal?(Date)
var.class.eql?(Date)
var.class.name == "Date"

# good
var.instance_of?(Date)

Ran it on 30k files and got 130 offenses. So seems useful.

Not sure if this should be in Style or Lint (looks like Style for me).

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 4, 2020

Yeah, I agree it's a style cop. Might be a good idea to mention this in the style guide as well.

| -
|===

This cop enforces the use of `Object#instance_of?` instead of class comparison
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, you can use kind_of? as well. I think we have some other cop for this, so that one should honor the config of the existing cop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind_of? is an alias for is_a?, which checks also superclasses. So this should be instance_of? for sure.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep mixing those up. :-)

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 4, 2020

You'll also need to rebase.

@fatkodima fatkodima force-pushed the class_equality_comparison-cop branch from 665cf42 to 78305af Compare October 4, 2020 18:13
@fatkodima
Copy link
Contributor Author

Rebased and added a link to style guide.

@bbatsov bbatsov merged commit 01cd876 into rubocop:master Oct 8, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 8, 2020

Thanks!

koic added a commit to koic/rubocop that referenced this pull request Jun 2, 2023
…yComparison`

Fixes rubocop#11914.

This PR fixes an incorrect examples for `Style/ClassEqualityComparison`.

The original `IgnoredMethods` (later renamed `AllowedMethods`) was introduced
by rubocop#8833. OTOH, rubocop#10809 seems to have mis-documented rubocop#8833.
bbatsov pushed a commit that referenced this pull request Jun 4, 2023
…ison`

Fixes #11914.

This PR fixes an incorrect examples for `Style/ClassEqualityComparison`.

The original `IgnoredMethods` (later renamed `AllowedMethods`) was introduced
by #8833. OTOH, #10809 seems to have mis-documented #8833.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants