Skip to content

Commit

Permalink
[Fix rubocop#147] Fix an error for Performance/AncestorsInclude
Browse files Browse the repository at this point in the history
Fixes rubocop#147.

This PR fixes an error for `Performance/AncestorsInclude`
when using `ancestors.include? without receiver.
  • Loading branch information
koic committed Jul 9, 2020
1 parent 809fae5 commit 1600d30
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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][])

## 1.7.0 (2020-07-07)

### New features
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.class'

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.class <= Klass
RUBY
end

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

0 comments on commit 1600d30

Please sign in to comment.