Skip to content

Style/HashExcept ignores negation #13012

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

Closed
lobner opened this issue Jun 22, 2024 · 0 comments · Fixed by #13015
Closed

Style/HashExcept ignores negation #13012

lobner opened this issue Jun 22, 2024 · 0 comments · Fixed by #13015
Labels

Comments

@lobner
Copy link

lobner commented Jun 22, 2024

Expected behavior

I expect it to alter the code correctly.

In this case, it could be slice -- the following lines of code behave the same.

Screenshot 2024-06-22 at 09 38 35

This is semantically the same.

Also, the Rails/NegateInclude - if run prior - would possibly solve it.

test_rubocop.rb:3:18: C: [Correctable] Rails/NegateInclude: Use .exclude? and remove the negation part.
address_array = ![:street_name, :house_number, :floor, :zipcode, :city, :country].include?(k)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Actual behavior

The Style/HashExcept cop dismisses the !

image

Steps to reproduce the problem

Create a file test_rubocop.rb and put the following lines in it:

 cat test_rubocop.rb
address_hash_1 = base_data.reject { |k, _v| ![:street_name, :house_number, :floor, :zipcode, :city, :country].include?(k) }
address_hash_2 = base_data.reject { |k, _v| [:street_name, :house_number, :floor, :zipcode, :city, :country].include?(k) }

Run: rubocop test_rubocop.rb --only Style/HashExcept and observe that it treats the lines as identical.

 rubocop test_rubocop.rb --only Style/HashExcept
Inspecting 1 file
C

Offenses:

test_rubocop.rb:1:26: C: [Correctable] Style/HashExcept: Use except(:street_name, :house_number, :floor, :zipcode, :city, :country) instead.
address_hash_1 = base_data.reject { |k, _v| ![:street_name, :house_number, :floor, :zipcode, :city, :country].include?(k) }
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test_rubocop.rb:2:28: C: [Correctable] Style/HashExcept: Use except(:street_name, :house_number, :floor, :zipcode, :city, :country) instead.
address_hash_2 = base_data.reject { |k, _v| [:street_name, :house_number, :floor, :zipcode, :city, :country].include?(k) }
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses autocorrectable

RuboCop version

➜ rubocop -V
1.64.1 (using Parser 3.3.3.0, rubocop-ast 1.31.3, running on ruby 3.2.4) [arm64-darwin23]
  - rubocop-capybara 2.21.0
  - rubocop-factory_bot 2.26.1
  - rubocop-minitest 0.35.0
  - rubocop-performance 1.21.1
  - rubocop-rails 2.25.0
  - rubocop-rake 0.6.0
@koic koic added the bug label Jun 22, 2024
@koic koic closed this as completed in 05f7738 Jun 24, 2024
koic added a commit that referenced this issue Jun 24, 2024
…h_except

[Fix #13012] Fix false positives for `Style/HashExcept`
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