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 #147] Fix an error for Performance/AncestorsInclude #148

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#147](https://github.com/rubocop-hq/rubocop-performance/issues/147): Fix an error for `Performance/AncestorsInclude` when using `ancestors.include?` without receiver. ([@koic][])

### Changes

* [#149](https://github.com/rubocop-hq/rubocop-performance/pull/149): Mark `Performance/AncestorsInclude` as unsafe. ([@eugeneius][])
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/performance/ancestors_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def on_send(node)
def autocorrect(node)
ancestors_include_candidate?(node) do |subclass, superclass|
lambda do |corrector|
corrector.replace(node, "#{subclass.source} <= #{superclass.source}")
subclass_source = subclass ? subclass.source : 'self'

corrector.replace(node, "#{subclass_source} <= #{superclass.source}")
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/performance/ancestors_include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
RUBY
end

it 'registers an offense and corrects when using `ancestors.include?` without receiver' do
expect_offense(<<~RUBY)
ancestors.include?(Klass)
^^^^^^^^^^^^^^^^^^ Use `<=` instead of `ancestors.include?`.
RUBY

expect_correction(<<~RUBY)
self <= Klass
RUBY
end

it 'does not register an offense when using `<=`' do
expect_no_offenses(<<~RUBY)
Class <= Kernel
Expand Down