rubocop icon indicating copy to clipboard operation
rubocop copied to clipboard

Add `Style/HashNewKeywordArguments` cop

Open r7kamura opened this issue 2 years ago • 4 comments

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}.md if the new code introduces user-observable changes. See changelog entry format for details.

r7kamura avatar Jan 26 '23 10:01 r7kamura

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.

r7kamura avatar Jan 26 '23 10:01 r7kamura

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.

koic avatar Jan 26 '23 11:01 koic

Let's mark this PR as WIP in the mean time.

bbatsov avatar Jan 30 '23 06:01 bbatsov

https://bugs.ruby-lang.org/issues/19236 has been closed by ruby/ruby#7828, which has been shipped in Ruby 3.3.

Zopolis4 avatar Jul 28 '24 06:07 Zopolis4

@r7kamura can you rebase this on top of master, so we can finally merge it?

bbatsov avatar May 29 '25 05:05 bbatsov

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

r7kamura avatar May 29 '25 06:05 r7kamura

There's significant overlap with the cop added in https://github.com/rubocop/rubocop/pull/13439. Is this PR needed anymore?

Earlopain avatar May 29 '25 06:05 Earlopain

Oh, I had forgotten about this one.

bbatsov avatar May 29 '25 06:05 bbatsov

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!

r7kamura avatar May 29 '25 06:05 r7kamura