Skip to content

Commit

Permalink
[Fix #9672] Fix an incorrect auto-correct for Style/HashConversion
Browse files Browse the repository at this point in the history
Fixes #9672.

This PR fixes an incorrect auto-correct for `Style/HashConversion`
when using  multi-argument `Hash[]` as a method argument.
  • Loading branch information
koic authored and bbatsov committed Apr 4, 2021
1 parent f4e72bc commit 50dc01e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
@@ -0,0 +1 @@
* [#9672](https://github.com/rubocop/rubocop/issues/9672): Fix an incorrect auto-correct for `Style/HashConversion` when using multi-argument `Hash[]` as a method argument. ([@koic][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/hash_conversion.rb
Expand Up @@ -85,6 +85,9 @@ def multi_argument(node)
else
add_offense(node, message: MSG_LITERAL_MULTI_ARG) do |corrector|
corrector.replace(node, args_to_hash(node.arguments))

parent = node.parent
add_parentheses(parent, corrector) if parent&.send_type? && !parent.parenthesized?
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/hash_conversion_spec.rb
Expand Up @@ -45,6 +45,17 @@
RUBY
end

it 'registers and corrects an offense when using multi-argument `Hash[]` as a method argument' do
expect_offense(<<~RUBY)
do_something Hash[a, b, c, d], arg
^^^^^^^^^^^^^^^^ Prefer literal hash to Hash[arg1, arg2, ...].
RUBY

expect_correction(<<~RUBY)
do_something({a => b, c => d}, arg)
RUBY
end

it 'does not try to correct multi-argument Hash with odd number of arguments' do
expect_offense(<<~RUBY)
Hash[a, b, c]
Expand Down

0 comments on commit 50dc01e

Please sign in to comment.