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 #159] Fix a false positive for Performance/AncestorsInclude #160

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 @@ -7,6 +7,10 @@
* [#140](https://github.com/rubocop-hq/rubocop-performance/pull/140): Add new `Performance/CollectionLiteralInLoop` cop. ([@fatkodima][])
* [#137](https://github.com/rubocop-hq/rubocop-performance/pull/137): Add new `Performance/Sum` cop. ([@fatkodima][])

### Bug fixes

* [#159](https://github.com/rubocop-hq/rubocop-performance/pull/159): Fix a false positive for `Performance/AncestorsInclude` when receiver is a variable. ([@koic][])

### Changes

* [#154](https://github.com/rubocop-hq/rubocop-performance/pull/154): Require RuboCop 0.87 or higher. ([@koic][])
Expand Down
16 changes: 11 additions & 5 deletions lib/rubocop/cop/performance/ancestors_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ class AncestorsInclude < Base

def on_send(node)
return unless (subclass, superclass = ancestors_include_candidate?(node))
return if subclass && !subclass.const_type?

location_of_ancestors = node.children[0].loc.selector.begin_pos
end_location = node.loc.selector.end_pos
range = range_between(location_of_ancestors, end_location)

add_offense(range) do |corrector|
add_offense(range(node)) do |corrector|
subclass_source = subclass ? subclass.source : 'self'

corrector.replace(node, "#{subclass_source} <= #{superclass.source}")
end
end

private

def range(node)
location_of_ancestors = node.children[0].loc.selector.begin_pos
end_location = node.loc.selector.end_pos

range_between(location_of_ancestors, end_location)
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/performance/ancestors_include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
RUBY
end

it 'does not register an offense when receiver is not a consntant' do
expect_no_offenses(<<~RUBY)
expect(object_one.ancestors.include?(object_two)).to eq(true)
RUBY
end

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