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 #4881] Fix a false positive for HashEachMethods when unused argument(s) exists in other blocks #4883

Merged
merged 1 commit into from
Oct 19, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Oct 19, 2017

Problem

Performance/HashEachMethods has a false positive for the following code.

@h.each do |key, value|
  p(key * -value)
end
@h.sum { |key, value| -value }

The each does not have any unused arguments. key and value are used. So, this cop should not add an offense for the code.
But the cop adds an offence for the code currently.
This cause is the sum has an unused argument, and the argument is named key, the name is same as the each's argument.
In this case, Performance/HashEachMethods adds a falsy offense.

Cause

The cop handles only argument names to detect unused. So the cop does not work properly if an unused argument that has same name exists in other block.

Solution

Check declared location of arguments instead of argument name.

Note

I found another bug while I was fixing this bug.
Auto-correction of this cop does not work properly.
For example:

 # Auto-correct the following code.
foo.each { |key, _v| p key }
 # Expected
foo.each_key { |key| p key }
 # Actual
foo.each_value { |key| p key }

This change will fix the bug also.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

…used argument(s) exists in other blocks

Problem
===

`Performance/HashEachMethods` has a false positive for the following code.

```ruby
@h.each do |key, value|
  p(key * -value)
end
@h.sum { |key, value| -value }
```

The `each` does not have any unused arguments. `key` and `value` are used. So, this cop should not add an offense for the code.
But the cop adds an offence for the code currently.
This cause is the `sum` has an unused argument, and the argument is named `key`, the name is same as the `each`'s argument.
In this case, `Performance/HashEachMethods` adds a falsy offense.

Cause
=====

The cop handles only argument names to detect unused. So the cop does not work properly if an unused argument that has same name exists in other block.

Solution
=====

Check declared location of arguments instead of argument name.

Note
====

I found another bug while I was fixing this bug.
Auto-correction of this cop does not work properly.
For example:

```ruby
 # Auto-correct the following code.
foo.each { |key, _v| p key }
 # Expected
foo.each_key { |key| p key }
 # Actual
foo.each_value { |key| p key }
```

This change will fix the bug also.
corrector.replace(node.loc.expression, new_source)
end

def correct_plain_each(node, corrector)
method = @args.include?(:k) ? :key : :value
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This :k is a cause of the auto-correction bug.

@bbatsov bbatsov merged commit 8751f74 into rubocop:master Oct 19, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 19, 2017

👍

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.

2 participants