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

[Fix #12370] Make Style/HashEachMethods aware of unused block value #12398

Conversation

koic
Copy link
Member

@koic koic commented Nov 18, 2023

Fixes #12370.

This PR makes Style/HashEachMethods aware of unused block value. And this PR suppresses the following new offense in this repository:

$ bundle exec rubocop --only Style/HashEachMethods -A
(snip)

Offenses:

lib/rubocop/cop/style/bisected_attr_accessor.rb:36:11: C: [Corrected] Style/HashEachMethods:
Use each_value instead of each, and remove the unused _visibility block argument.
          find_macros(class_node.body).each do |_visibility, macros| ...
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1519 files inspected, 1 offense detected, 1 offense corrected

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • 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.

@koic koic force-pushed the make_hash_each_methods_aware_of_unused_block_value branch from 5ac7cac to 23a6ba2 Compare November 20, 2023 08:11
@@ -47,6 +47,40 @@
RUBY
end

it 'does not register an offense when the value and value block arguments of `Enumerable#each` method are unused' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value and value -> key are value

unused -> used

expect_no_offenses('foo.each { |k, v| do_something(k, v) }')
end

it 'does not register an offense when the single block argument of `Enumerable#each` method are unused' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused -> used

expect_no_offenses('foo.each { |e| do_something(e) }')
end

it 'does not register an offense when the parenthesized value and value block arguments of `Enumerable#each` method are unused' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused -> used

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value and value -> key and value

expect_no_offenses('foo.each { |(k, v)| do_something(e) }')
end

it 'registers an offense when the value block argument of `Enumerable#each` method are unused' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are -> is

RUBY
end

it 'registers an offense when the key block argument of `Enumerable#each` method are unused' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are -> is

@koic koic force-pushed the make_hash_each_methods_aware_of_unused_block_value branch from 23a6ba2 to 7813213 Compare November 20, 2023 08:20
#
# # bad
# hash.values.each { |v| p v }
# hash.each.each { |unused_key, v| p v }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be just hash.each here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I've fixed it.

…k value

Fixes rubocop#12370.

This PR makes `Style/HashEachMethods` aware of unused block value.
And this PR suppresses the following new offense in this repository:

```console
$ bundle exec rubocop --only Style/HashEachMethods -A
(snip)

Offenses:

lib/rubocop/cop/style/bisected_attr_accessor.rb:36:11: C: [Corrected] Style/HashEachMethods:
Use each_value instead of each, and remove the unused _visibility block argument.
          find_macros(class_node.body).each do |_visibility, macros| ...
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1519 files inspected, 1 offense detected, 1 offense corrected
```
@koic koic force-pushed the make_hash_each_methods_aware_of_unused_block_value branch from 7813213 to b42f1a0 Compare November 21, 2023 02:44
@bbatsov bbatsov merged commit de86a68 into rubocop:master Nov 21, 2023
28 checks passed
@koic koic deleted the make_hash_each_methods_aware_of_unused_block_value branch November 21, 2023 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Style/HashEachMethods: Detect use of .each + _ variables
3 participants