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

Add Performance/InefficientHashSearch cop #5848

Merged
merged 3 commits into from
May 5, 2018
Merged

Add Performance/InefficientHashSearch cop #5848

merged 3 commits into from
May 5, 2018

Conversation

JacobEvelyn
Copy link
Contributor

Usage of Hash#keys.include? can be replaced with the
much more efficient Hash#key? method. Similarly,
Hash#values.include? can be replaced with the more
efficient Hash#value? method.

This commit adds the Performance/InefficientHashSearch
cop, which identifies and autocorrects these
inefficient hash searches.


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).
  • 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.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.

Usage of `Hash#keys.include?` can be replaced with the
much more efficient `Hash#key?` method. Similarly,
`Hash#values.include?` can be replaced with the more
efficient `Hash#value?` method.

This commit adds the `Performance/InefficientHashSearch`
cop, which identifies and autocorrects these
inefficient hash searches.
PATTERN

def on_send(node)
add_offense(node, message: KEYS_MSG) if keys_include?(node)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could maybe be an if-else, but I don't know enough about how ruby_parser works to say that one node couldn't potentially have both issues. Feedback welcome!

lambda do |corrector|
# Replace `keys.include?` or `values.include?` with the appropriate
# `key?`/`value?` method.
corrector.replace(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if this is the best way to do this autocorrect; whitespace gets swallowed and the new code will have parens even if the original include? did not. Feedback welcome!

Copy link
Contributor

Choose a reason for hiding this comment

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

You could probably try (from the top of my head):

receiver, include_method, _arg = *node
_, keys_or_values = *receiver
corrector.replace(include_method.source_range, '') # Maybe there's a `corrector.delete` ?
corrector.replace(keys_or_values.source_range, autocorrect_method(keys_or_values))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This already merged but I'll play around with this approach next time!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, just came across this PR while checking the changelog and saw your comment without any reply

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really appreciate it!


def autocorrect_method(node)
old_method = node.children[0].loc.selector.source
old_method == 'keys' ? 'key?' : 'value?'
Copy link
Contributor Author

@JacobEvelyn JacobEvelyn May 1, 2018

Choose a reason for hiding this comment

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

I wasn't sure if this should incorporate the codebase's Style/PreferredHashMethods preference (and potentially use has_key? and has_value? instead). Or whether autocorrect runs in multiple passes so that would get corrected anyway. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Great question. We try to avoid having one cop produce offenses in another cop. Checking the configuration of Style/PreferredHashMethods and using the short or verbose form would be preferred.

@@ -983,6 +983,11 @@ Performance/FlatMap:
# This can be dangerous since `flat_map` will only flatten 1 level, and
# `flatten` without any parameters can flatten multiple levels.

Performance/InefficientHashSearch:
Description: 'Use `key?` or `value?` instead of `keys.include?` or `values.include?`'
Reference: 'https://github.com/JuanitoFatas/fast-ruby#hashkey-instead-of-hashkeysinclude-and-hashvalue-instead-of-hashvaluesinclude-code'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This reference doesn't exist yet, but I have an outstanding PR which will create it: fastruby/fast-ruby#154

@JacobEvelyn
Copy link
Contributor Author

This is my first RuboCop PR and I'd appreciate any feedback!


def autocorrect_method(node)
old_method = node.children[0].loc.selector.source
old_method == 'keys' ? 'key?' : 'value?'
Copy link
Contributor

Choose a reason for hiding this comment

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

Great question. We try to avoid having one cop produce offenses in another cop. Checking the configuration of Style/PreferredHashMethods and using the short or verbose form would be preferred.

private

def autocorrect_method(node)
old_method = node.children[0].loc.selector.source
Copy link
Contributor

Choose a reason for hiding this comment

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

node.children[0].method_name could be used here. It will return a symbol instead of a string for the method name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip! Changed.

PATTERN

def_node_matcher :values_include?, <<-PATTERN
(send (send _ :values) :include? _)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can probably simplify some of the auto-correct logic by using $ as a capture in the node pattern.

(send $(send _ ${:keys :values}) :include? $_)
The first return would be node.children[0].
The second return would be the method name.
The third return would be the arguments being passed to include?.

If you go this route, the patterns can be combined into a single matcher and the message can be determined based off the method name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the tip! Changed.

@JacobEvelyn
Copy link
Contributor Author

Thanks for all the feedback, @rrosenblum! Just pushed a commit that addresses your comments.

#
class InefficientHashSearch < Cop
def_node_matcher :inefficient_include?, <<-PATTERN
(send $(send _ ${:keys :values}) :include? $_)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the cop also check and fix exclude? ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

exclude is in ActiveSupport, so probably not.

@bbatsov bbatsov merged commit e270c7b into rubocop:master May 5, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented May 5, 2018

Great first contribution! Keep them coming! 😉

@JacobEvelyn
Copy link
Contributor Author

Thanks!

@JacobEvelyn JacobEvelyn deleted the inefficient-hash-searching branch May 5, 2018 14: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.

None yet

5 participants