Add `Style/HashNewKeywordArguments` cop
I have heard that future versions of Ruby are likely to deprecate the ability to pass default values with keyword arguments to Hash.new.
- https://bugs.ruby-lang.org/issues/19236
So I thought it would be a good idea to prepare a Cop that detects such usage and autocorrects it while it is still available. Since the signature is originally Hash.new(default_value = nil), there should be no problem deprecating the keyword arguments.
- https://ruby-doc.org/3.1.2/Hash.html#method-c-new
Before submitting the PR make sure the following are checked:
- [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
- [x] Wrote good commit messages.
- [ ] Commit message starts with
[Fix #issue-number](if the related issue exists). - [x] Feature branch is up-to-date with
master(if not - rebase it). - [x] Squashed related commits together.
- [x] Added tests.
- [x] Ran
bundle exec rake default. It executes all tests and runs RuboCop on its own code. - [x] Added an entry (file) to the changelog folder named
{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.
I am a little concerned about whether Style is the correct department. Semantically, Lint seems to be more correct, but I chose Style for now because I think Lint department is the place to put the most urgent Cop.
Interesting. However, it is too early to judge the direction of this cop force yet. So, it should be decided after https://bugs.ruby-lang.org/issues/19236 is settled. Let's come back to this PR once https://bugs.ruby-lang.org/issues/19236 is closed.
Let's mark this PR as WIP in the mean time.
https://bugs.ruby-lang.org/issues/19236 has been closed by ruby/ruby#7828, which has been shipped in Ruby 3.3.
@r7kamura can you rebase this on top of master, so we can finally merge it?
Thanks for the reminder 👍
I've resolved the conflict and rebased onto the latest master. It looks like there was a conflict due to the addition of a similarly named Style/HashSlice cop.
- https://github.com/rubocop/rubocop/pull/13507
There's significant overlap with the cop added in https://github.com/rubocop/rubocop/pull/13439. Is this PR needed anymore?
Oh, I had forgotten about this one.
Nice catch. I was completely unaware of its existence as well 😆
The purpose is the same, but in terms of the thoroughness of the explanation and the consideration for edge cases like capacity, Lint/HashNewWithKeywordArgumentsAsDefault is definitely superior.
I'll go ahead and close this pull request — thanks again!