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

Fix an error for Rails/NegateInclude when there is no receiver #920

Merged
merged 1 commit into from
Jan 24, 2023

Conversation

fatkodima
Copy link
Contributor

This PR fixes an error when there is no receiver. And handles the case when it is used within exclude? method (because correcting would cause infinite recursion).

Example from rails: https://github.com/rails/rails/blob/d75fdd1c2f6002d62e5ecf65dda2942065f071f0/activesupport/lib/active_support/core_ext/string/exclude.rb#L10-L12

private

def within_exclude_method?(node)
def_node = node.each_ancestor(:def, :defs).first
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for :defs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

def self.foo
!include?(2)
^^^^^^^^^^^^ Use `.exclude?` and remove the negation part.
end
Copy link
Member

Choose a reason for hiding this comment

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

That's what I noticed after reconsidering. It looks better to allow if there is no receiver because there may be user-defined include?, which may not have exclude?.

And can you separate the tests for def and defs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to not raise an offense when there is no receiver. Now this PR is tiny 👍.
And that was actually an overkill from my side to detect cases like !include? inside def exclude?.

@koic koic merged commit 0449e6d into rubocop:master Jan 24, 2023
@koic
Copy link
Member

koic commented Jan 24, 2023

Great! Thank you!

@fatkodima fatkodima deleted the negate_include-no-receiver branch January 24, 2023 16:56
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.

2 participants