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/ArrayCoercion does not play nicely with Hashes #8783

Closed
adamcooke opened this issue Sep 24, 2020 · 3 comments · Fixed by #8792
Closed

Style/ArrayCoercion does not play nicely with Hashes #8783

adamcooke opened this issue Sep 24, 2020 · 3 comments · Fixed by #8792

Comments

@adamcooke
Copy link

Expected behaviour

The Style/ArrayCoercion is not safe because it converts hashes to arrays.

Actual behaviour

values = [values] unless values.is_a?(Array)
# is corrected to
values = Array(values)

Which leads to values being:

values = {a: 'b'}
values = Array(values) # => [[:a, "b"]] but it should be [{a: 'b'}]

RuboCop version

$ bundle exec rubocop -V
0.91.1 (using Parser 2.7.1.4, rubocop-ast 0.4.2, running on ruby 2.6.5 x86_64-linux)
@marcandre
Copy link
Contributor

Indeed. See #8391 and #8334

We're waiting for @bbatsov to decide to retire this Cop, or at least have a config so it targets [*val] but not val = [val] unless val.is_a?(Array) 😅

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 25, 2020

The main reason I didn't rush into a decision is that the cop has been around for a while and there are relatively few complaints which leads me to believe it's useful in most cases. If only Ruby's internals were more consistent. 😆

I'm leaning towards simply disabling the cop, so that people can still run it on demand when needed, but some thoughts from others in @rubocop-hq/rubocop-core are welcome.

@koic
Copy link
Member

koic commented Sep 25, 2020

I agree to disable the cop by default. Because users who want to detect the style guide rule in exchange for false positives may want to enable it.

koic added a commit to koic/rubocop that referenced this issue Sep 25, 2020
Fixes rubocop#8783, rubocop#8391, and rubocop#8334.

This PR disables `Style/ArrayCoercion` cop by default.
Because false positive will occur if the argument of `Array()` is not an array (e.g. Hash, Set),
an array will be returned as an incompatibility result.
bbatsov pushed a commit that referenced this issue Sep 25, 2020
Fixes #8783, #8391, and #8334.

This PR disables `Style/ArrayCoercion` cop by default.
Because false positive will occur if the argument of `Array()` is not an array (e.g. Hash, Set),
an array will be returned as an incompatibility result.
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.

4 participants