Skip to content

Commit

Permalink
Merge pull request #8682 from eugeneius/hash_transformation_hash_reuse
Browse files Browse the repository at this point in the history
Fix a false positive for Style/HashTransformKeys and Style/HashTransformValues
  • Loading branch information
koic committed Sep 10, 2020
2 parents efcb004 + 04df10b commit 429b696
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@
* [#8664](https://github.com/rubocop-hq/rubocop/issues/8664): Fix a false positive for `Naming/BinaryOperatorParameterName` when naming multibyte character method name. ([@koic][])
* [#8604](https://github.com/rubocop-hq/rubocop/issues/8604): Fix a false positive for `Bundler/DuplicatedGem` when gem is duplciated in condition. ([@tejasbubane][])
* [#8671](https://github.com/rubocop-hq/rubocop/issues/8671): Fix an error for `Style/ExplicitBlockArgument` when using safe navigation method call. ([@koic][])
* [#8682](https://github.com/rubocop-hq/rubocop/pull/8682): Fix a positive for `Style/HashTransformKeys` and `Style/HashTransformValues` when the `each_with_object` hash is used in the transformed key or value. ([@eugeneius][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_transform_keys.rb
Expand Up @@ -40,7 +40,7 @@ class HashTransformKeys < Base
(arg $_)
(arg _val))
(arg _memo))
({send csend} (lvar _memo) :[]= $_ $(lvar _val)))
({send csend} (lvar _memo) :[]= $!`_memo $(lvar _val)))
PATTERN

def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_transform_values.rb
Expand Up @@ -37,7 +37,7 @@ class HashTransformValues < Base
(arg _key)
(arg $_))
(arg _memo))
({send csend} (lvar _memo) :[]= $(lvar _key) $_))
({send csend} (lvar _memo) :[]= $(lvar _key) $!`_memo))
PATTERN

def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/hash_transform_keys_spec.rb
Expand Up @@ -65,6 +65,12 @@
RUBY
end

it 'does not flag `each_with_object` when its argument is used in the key' do
expect_no_offenses(<<~RUBY)
x.each_with_object({}) { |(k, v), h| h[h[k.to_sym]] = v }
RUBY
end

it 'does not flag each_with_object when its receiver is array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].each_with_object({}) {|(k, v), h| h[foo(k)] = v}
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/hash_transform_values_spec.rb
Expand Up @@ -64,6 +64,12 @@
RUBY
end

it 'does not flag `each_with_object` when its argument is used in the value' do
expect_no_offenses(<<~RUBY)
x.each_with_object({}) { |(k, v), h| h[k] = h.count }
RUBY
end

it 'does not flag each_with_object when receiver is array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].each_with_object({}) {|(k, v), h| h[k] = foo(v)}
Expand Down

0 comments on commit 429b696

Please sign in to comment.