diff --git a/CHANGELOG.md b/CHANGELOG.md index 1add5ffa1009..77159aa8d87c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/performance/redundant_merge.rb b/lib/rubocop/cop/performance/redundant_merge.rb index e4fca6a0c87e..dcd9fa1857f1 100644 --- a/lib/rubocop/cop/performance/redundant_merge.rb +++ b/lib/rubocop/cop/performance/redundant_merge.rb @@ -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? diff --git a/spec/rubocop/cop/performance/redundant_merge_spec.rb b/spec/rubocop/cop/performance/redundant_merge_spec.rb index dbed70f06fd5..b260d5c1d00d 100644 --- a/spec/rubocop/cop/performance/redundant_merge_spec.rb +++ b/spec/rubocop/cop/performance/redundant_merge_spec.rb @@ -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|',