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 positive for Style/HashTransformKeys and Style/HashTransformValues #8682

Merged
merged 1 commit into from Sep 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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