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 a false negative for Performance/BindCall #210

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
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