Skip to content

Commit

Permalink
[Fix rubocop#6545] Fix Performance/RedundantMerge kwsplat regression
Browse files Browse the repository at this point in the history
  • Loading branch information
mmedal committed Dec 4, 2018
1 parent 52319e4 commit 8b4e144
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* [#6511](https://github.com/rubocop-hq/rubocop/issues/6511): Fix an incorrect auto-correct for `Style/EmptyCaseCondition` when used as an argument of a method. ([@koic][])
* [#6509](https://github.com/rubocop-hq/rubocop/issues/6509): Fix an incorrect auto-correct for `Style/RaiseArgs` when an exception object is assigned to a local variable. ([@koic][])
* [#6534](https://github.com/rubocop-hq/rubocop/issues/6534): Fix a false positive for `Lint/UselessAccessModifier` when using `private_class_method`. ([@dduugg][])
* [#6545](https://github.com/rubocop-hq/rubocop/issues/6545): Fix a regression where `Performance/RedundantMerge` raises an error on a sole double splat argument passed to `merge!`. ([@mmedal][])

### Changes

Expand Down Expand Up @@ -3670,3 +3671,4 @@
[@takaram]: https://github.com/takaram
[@gmcgibbon]: https://github.com/gmcgibbon
[@dduugg]: https://github.com/dduugg
[@mmedal]: https://github.com/mmedal
5 changes: 5 additions & 0 deletions lib/rubocop/cop/performance/redundant_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,18 @@ def each_redundant_merge(node)

def non_redundant_merge?(node, receiver, pairs)
non_redundant_pairs?(receiver, pairs) ||
kwsplat_used?(pairs) ||
non_redundant_value_used?(receiver, node)
end

def non_redundant_pairs?(receiver, pairs)
pairs.size > 1 && !receiver.pure? || pairs.size > max_key_value_pairs
end

def kwsplat_used?(pairs)
pairs.any?(&:kwsplat_type?)
end

def non_redundant_value_used?(receiver, node)
node.value_used? &&
!EachWithObjectInspector.new(node, receiver).value_used?
Expand Down
10 changes: 9 additions & 1 deletion spec/rubocop/cop/performance/redundant_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@
end

context 'when any argument is a double splat' do
it 'does not register an offense' do
it 'does not register an offense when the only argument is a' \
'double splat' do
expect_no_offenses(<<-RUBY.strip_indent)
foo.merge!(**bar)
RUBY
end

it 'does not register an offense when there are multiple arguments ' \
'and at least one is a double splat' do
expect_no_offenses(<<-RUBY.strip_indent)
foo.merge!(baz: qux, **bar)
RUBY
Expand Down

0 comments on commit 8b4e144

Please sign in to comment.