Skip to content

Commit

Permalink
[Fix rubocop#3430] Prevent exception in Performance/RedundantMerge
Browse files Browse the repository at this point in the history
…when receiver is implicit

This cop would blow up when inspecting code that involved sending
`#merge!` to `self` implicitly, e.g.:

```
class Foo
  def bar(baz)
    merge!(baz)
  end
end
```

This change fixes that.
  • Loading branch information
Drenmi committed Sep 30, 2016
1 parent 1044fa4 commit 619eef7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [#3540](https://github.com/bbatsov/rubocop/issues/3540): Fix `Style/GuardClause` to register offense for instance and singleton methods. ([@tejasbubane][])
* [#3311](https://github.com/bbatsov/rubocop/issues/3311): Detect incompatibilities with the external encoding to prevent bad autocorrections in `Style/StringLiterals`. ([@deivid-rodriguez][])
* [#3499](https://github.com/bbatsov/rubocop/issues/3499): Ensure `Lint/UnusedBlockArgument` doesn't make recommendations that would change arity for methods defined using `#define_method`. ([@drenmi][])
* [#3430](https://github.com/bbatsov/rubocop/issues/3430): Fix exception in `Performance/RedundantMerge` when inspecting a `#merge!` with implicit receiver. ([@drenmi][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/redundant_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def autocorrect(node)

def each_redundant_merge(node)
redundant_merge(node) do |receiver, pairs|
next unless receiver
next if node.value_used? &&
!EachWithObjectInspector.new(node, receiver).value_used?
next if pairs.size > 1 && !receiver.pure?
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/performance/redundant_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
end
end

context 'when receiver is implicit' do
it "doesn't autocorrect" do
new_source = autocorrect_source(cop, 'merge!(foo: 1, bar: 2)')
expect(new_source).to eq('merge!(foo: 1, bar: 2)')
end
end

context 'when internal to each_with_object' do
it 'autocorrects when the receiver is the object being built' do
source = ['foo.each_with_object({}) do |f, hash|',
Expand Down

0 comments on commit 619eef7

Please sign in to comment.