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 an incorrect auto-correct for Style/HashConversion #9712

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
@@ -0,0 +1 @@
* [#9712](https://github.com/rubocop/rubocop/pull/9712): Fix an incorrect auto-correct for `Style/HashConversion` when `Hash[]` as a method argument without parentheses. ([@koic][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/hash_conversion.rb
Expand Up @@ -84,6 +84,9 @@ def use_zip_method_without_argument?(first_argument)
def register_offense_for_hash(node, hash_argument)
add_offense(node, message: MSG_LITERAL_HASH_ARG) do |corrector|
corrector.replace(node, "{#{hash_argument.source}}")

parent = node.parent
add_parentheses(parent, corrector) if parent&.send_type? && !parent.parenthesized?
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/hash_conversion_spec.rb
Expand Up @@ -34,6 +34,28 @@
RUBY
end

it 'reports different offense for hash argument Hash[] as a method argument with parentheses' do
expect_offense(<<~RUBY)
do_something(Hash[a: b, c: d], 42)
^^^^^^^^^^^^^^^^ Prefer literal hash to Hash[key: value, ...].
RUBY

expect_correction(<<~RUBY)
do_something({a: b, c: d}, 42)
RUBY
end

it 'reports different offense for hash argument Hash[] as a method argument without parentheses' do
expect_offense(<<~RUBY)
do_something Hash[a: b, c: d], 42
^^^^^^^^^^^^^^^^ Prefer literal hash to Hash[key: value, ...].
RUBY

expect_correction(<<~RUBY)
do_something({a: b, c: d}, 42)
RUBY
end

it 'reports different offense for empty Hash[]' do
expect_offense(<<~RUBY)
Hash[]
Expand Down