-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add new Style/HashExcept
cop
#9283
Conversation
You're fast 😆 Technically, it's only if using
Maybe if running in safe mode we should be more careful, so |
7ac8d16
to
38c767d
Compare
Oh, I couldn't catch it! I prefer being safe, so I updated this PR that way! For safe detection, it is limited to commonly used string and symbol comparisons when used |
lib/rubocop/cop/style/hash_except.rb
Outdated
minimum_target_ruby_version 3.0 | ||
|
||
MSG = 'Use `%<prefer>s` instead.' | ||
RESTRICT_ON_SEND = %i[reject select].freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you forgot about filter
, keep_if
and delete_if
. I don't use those Hash methods often, but I recall there were a ton of aliases there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added filter
method. On the other hand, delete_if
and keep_if
methods are not checked because they are incompatible with except
method.
# except
h = {foo: 1, bar: 2, baz: 3}
=> {:foo=>1, :bar=>2, :baz=>3}
h.except(:bar)
=> {:foo=>1, :baz=>3}
h
=> {:foo=>1, :bar=>2, :baz=>3}
# delete_if
h = {foo: 1, bar: 2, baz: 3}
=> {:foo=>1, :bar=>2, :baz=>3}
h.delete_if {|k, v| k == :bar }
=> {:foo=>1, :baz=>3}
h
=> {:foo=>1, :baz=>3}
# keep_if
h = {foo: 1, bar: 2, baz: 3}
=> {:foo=>1, :bar=>2, :baz=>3}
h.keep_if {|k, v| k != :bar }
=> {:foo=>1, :baz=>3}
h
=> {:foo=>1, :baz=>3}
I updated documentation and test on this.
38c767d
to
678f147
Compare
This PR adds new `Style/HashExcept` cop. This cop checks for usages of `Hash#reject`, `Hash#select`, and `Hash#filter` methods that can be replaced with `Hash#except` method. This cop should only be enabled on Ruby version 3.0 or higher. (`Hash#except` was added in Ruby 3.0.) ```ruby # bad {foo: 1, bar: 2, baz: 3}.reject {|k, v| k == :bar } {foo: 1, bar: 2, baz: 3}.select {|k, v| k != :bar } # good {foo: 1, bar: 2, baz: 3}.except(:bar) ``` cf. https://bugs.ruby-lang.org/issues/15822
678f147
to
6166df3
Compare
Thanks! |
@koic Question about this new cop. I've noticed this pulling through on ruby 2.7 - However it's pulling through as Just saw the ruby lang implementation for ruby 2.7 - So I'm just guessing this was in AS before that? |
This PR adds new
Style/HashExcept
cop.This cop checks for usages of
Hash#reject
andHash#select
methods that can be replaced withHash#except
method.This cop should only be enabled on Ruby version 3.0 or higher.
(
Hash#except
was added in Ruby 3.0.)cf. https://bugs.ruby-lang.org/issues/15822
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.