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/StringHashKeys is not safe for autocorrection #6980

Closed
fig opened this issue Apr 27, 2019 · 1 comment · Fixed by #7031
Closed

Style/StringHashKeys is not safe for autocorrection #6980

fig opened this issue Apr 27, 2019 · 1 comment · Fixed by #7031

Comments

@fig
Copy link

fig commented Apr 27, 2019

I don't believe that Style/StringHashKeys should be labeled as safe for autocorrection.

As an example, a Hash may be passed as the second argument to String#gsub. If the matched text is one of its keys, the corresponding value is the replacement string. Converting the String key to a Symbol will break the replacement.


Steps to reproduce the problem

example scenario:

"The sky is green.".gsub(/green/, "green" => "blue")
# => "The sky is blue."

after auto correct:

"The sky is green.".gsub(/green/, green: "blue")
# => "The sky is ."

RuboCop version

$ rubocop -V
0.67.2 (using Parser 2.6.2.1, running on ruby 2.6.3 x86_64-linux)

A solution might be to ignore StringHashKeys if they occur as an argument to #gsub / #gsub!

tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 9, 2019
@tejasbubane
Copy link
Contributor

There are existing exceptions in Style/StringHashKeys for IO.popen, etc. So I added gsub and gsub! to the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants