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

Style/HashConversion cop is unsafe for arguments that are not enclosed in parentheses. #9672

1grc opened this issue Apr 3, 2021 · 0 comments


Copy link

@1grc 1grc commented Apr 3, 2021

Expected behavior

Safe auto correct of Style/HashConversion should not introduce errors in the code.

Actual behavior

If the auto-correct target is an argument without parentheses, the line will be interpreted as a SyntaxError or One-line pattern matching after correct.

Steps to reproduce the problem

Create example.rb :

# frozen_string_literal: true

puts Hash['a', 'b']

Next, run the following to reproduce the error:

bundle exec rubocop --debug --enable-pending-cops -a example.rb
$ bundle exec rubocop --debug --enable-pending-cops -a example.rb
For /home/kazuma/repo/test: Default configuration from /home/kazuma/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/rubocop-1.12.0/config/default.yml
Inspecting 1 file
Scanning /home/kazuma/repo/test/example.rb


example.rb:3:6: C: [Corrected] Style/HashConversion: Prefer literal hash to Hash[arg1, arg2, ...].
puts Hash['a', 'b']
example.rb:3:11: E: Lint/Syntax: unexpected token tASSOC
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
puts {'a' => 'b'}

1 file inspected, 2 offenses detected, 1 offense corrected
Finished in 0.14253549999557436 seconds
# example.rb

# frozen_string_literal: true
puts {'a' => 'b'}
$ ruby example.rb 
example.rb:3: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!

RuboCop version

$ bundle exec rubocop -V
1.12.0 (using Parser, rubocop-ast 1.4.1, running on ruby 3.0.0 x86_64-linux)
@koic koic added the bug label Apr 3, 2021
koic added a commit to koic/rubocop that referenced this issue Apr 3, 2021

Fixes rubocop#9672.

This PR fixes an incorrect auto-correct for `Style/HashConversion`
when using  multi-argument `Hash[]` as a method argument.
@bbatsov bbatsov closed this in #9673 Apr 4, 2021
bbatsov added a commit that referenced this issue Apr 4, 2021
Fixes #9672.

This PR fixes an incorrect auto-correct for `Style/HashConversion`
when using  multi-argument `Hash[]` as a method argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants