Skip to content

Commit

Permalink
Merge pull request #160 from koic/fix_false_positive_for_performance_…
Browse files Browse the repository at this point in the history
…ancestors_include

[Fix #159] Fix a false positive for `Performance/AncestorsInclude`
  • Loading branch information
koic authored Aug 30, 2020
2 parents e973c7c + e69d586 commit 5792ef0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
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

0 comments on commit 5792ef0

Please sign in to comment.