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 auto-correction not equal for nil values #8391
Comments
Agreed, this cop is, at the very least, not safe. Thanks for raising this issue. Any object that Array({a: 1}) # => [[:a, 1]] != [{a: 1}]
Array(Set[1, 2, 3]) # => [1, 2, 3] != [Set[1, 2, 3]]
Array(1..) # => RangeError ! = [1..] I'll make it as unsafe but the issue should remain open: does this cop deserve its badge? |
What about using
|
Ah, it won't work for arrays args as expected :( |
This one seems to work https://stackoverflow.com/a/29431497
|
There just isn't a reliable builtin way to do this. Maybe this cop could enforce the use of proper duck-typing: # bad?
x = [x] unless x.is_a?(Array)
# good
x = [x] unless x.respond_to? :to_ary Note: I did mean to use Given the fact that |
Just checking whether x = x.respond_to?(:to_ary) ? x.to_ary : [x] IMO, +1 for retiring this cop |
There's also the option of simply disabling the cop by default, which is a bit less extreme (at least people can run it on demand then). The fact that relatively few people complained about running into conversion issues leads me to believe that's not a very common problem, but that' definitely a cop that can't be made safe. |
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.
Now disabled by default #8792 |
If a value can be nil, then ArrayCoercion auto-corrected change does not produce an equivalent change. It's a good suggestion but not always valid.
Expected behavior
Don't auto-correct unless certain
Actual behavior
Changes code
Steps to reproduce the problem
RuboCop version
Include the output of
rubocop -V
orbundle exec rubocop -V
if using Bundler. Here's an example:The text was updated successfully, but these errors were encountered: