Skip to content

Commit

Permalink
Fix a false negative for Performance/BindCall
Browse files Browse the repository at this point in the history
This PR fix a false negative for `Performance/BindCall`
when receiver is not a method call.

I heard that it could not be detected by the following changes in rails/rails.
rails/rails@6515d69
  • Loading branch information
koic committed Feb 9, 2021
1 parent 4fe6edd commit 71cd838
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug fixes

* [#207](https://github.com/rubocop-hq/rubocop-performance/issues/207): Fix an error for `Performance/Sum` when using `map(&do_something).sum` without receiver. ([@koic][])
* [#210](https://github.com/rubocop-hq/rubocop-performance/pull/210): Fix a false negative for `Performance/BindCall` when receiver is not a method call. ([@koic][])

### Changes

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/performance/bind_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BindCall < Base
def_node_matcher :bind_with_call_method?, <<~PATTERN
(send
$(send
(send nil? _) :bind
_ :bind
$(...)) :call
$...)
PATTERN
Expand Down Expand Up @@ -64,7 +64,7 @@ def message(bind_arg, call_args)

def correction_range(receiver, node)
location_of_bind = receiver.loc.selector.begin_pos
location_of_call = node.loc.end.end_pos
location_of_call = node.source_range.end.end_pos

range_between(location_of_bind, location_of_call)
end
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/performance/bind_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@
RUBY
end

it 'registers an offense when using `Foo.do_something.bind(obj).call`' do
expect_offense(<<~RUBY)
Foo.do_something.bind(obj).call
^^^^^^^^^^^^^^ Use `bind_call(obj)` instead of `bind(obj).call()`.
RUBY

expect_correction(<<~RUBY)
Foo.do_something.bind_call(obj)
RUBY
end

it 'registers an offense when using `CONSTANT.bind(obj).call`' do
expect_offense(<<~RUBY)
CONSTANT.bind(obj).call
^^^^^^^^^^^^^^ Use `bind_call(obj)` instead of `bind(obj).call()`.
RUBY

expect_correction(<<~RUBY)
CONSTANT.bind_call(obj)
RUBY
end

it 'registers an offense when using `bind(obj).()`' do
expect_offense(<<~RUBY)
umethod.bind(obj).()
Expand Down

0 comments on commit 71cd838

Please sign in to comment.