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

False positive and unsafe correction with Style/HashAsLastArrayItem and double splat operator. #11161

Closed
inkstak opened this issue Nov 7, 2022 · 1 comment · Fixed by #11162
Labels

Comments

@inkstak
Copy link

inkstak commented Nov 7, 2022

Expected behavior

Using the double splat operator is very convenient to avoid adding an empty hash to an array.

def build_args(options = {})
  [1, 2, 3, **options]
end

> build_args
 => [1, 2, 3]

> build_args(foo: :bar)
 => [1, 2, 3, {:foo=>:bar}]

Actual behavior

With the default rubocop settings:

Style/HashAsLastArrayItem:
  EnforcedStyle: braces

The actual behavior reports an offense.

example.rb:2:13: C: [Correctable] Style/HashAsLastArrayItem: Wrap hash in { and }. (https://rubystyle.guide#hash-literal-as-last-array-item)
  [1, 2, 3, **options]
            ^^^^^^^^^

It also autocorrects the statement to change the behavior:

def build_args(options = {})
  [1, 2, 3, { **options }]
end

RuboCop version

$ [bundle exec] rubocop -V
1.38.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 2.7.6) +server [arm64-darwin21]
  - rubocop-performance 1.15.0
  - rubocop-rails 2.17.2
  - rubocop-rspec 2.14.2
@koic koic added the bug label Nov 7, 2022
koic added a commit to koic/rubocop that referenced this issue Nov 7, 2022
Fixes rubocop#11161.

This PR fixes a false positive for `Style/HashAsLastArrayItem`
when using double splat operator.
koic added a commit that referenced this issue Nov 8, 2022
…sh_as_last_array_item

[Fix #11161] Fix a false positive for `Style/HashAsLastArrayItem`
@inkstak
Copy link
Author

inkstak commented Nov 8, 2022

Thanks !

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

Successfully merging a pull request may close this issue.

2 participants