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 new Performance/RegexpMatch cop #3824

Merged
merged 1 commit into from
Dec 26, 2016
Merged

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Dec 25, 2016

In Ruby 2.4, String#match? and Regexp#match? have been added.
https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/
https://bugs.ruby-lang.org/issues/8110

The methods are faster than match.
Because the methods avoid creating a MatchData object or saving backref.
So, when MatchData is not used, use match? instead of match.

This cop suggests using match? method.


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 are passing.
  • The new code doesn't generate RuboCop offenses.
  • 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).

PATTERN

def on_if(node)
return if target_ruby_version < 2.4
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably we simply need to add to the Cop class some method like minimum_target_ruby_version or something. I hate having to write such checks all over the place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good. I think it should be declarative as a class method.

For example.

module RuboCop
  ...
    class SomeCop
      minimum_target_ruby_version 2.4
    end
  ...
end

I'll implement it in other pull-request.

@bbatsov
Copy link
Collaborator

bbatsov commented Dec 25, 2016

Check the commit message. :-)

@pocke pocke changed the title Add new Performance/RegexpMerge cop Add new Performance/RegexpMatch cop Dec 25, 2016
@pocke
Copy link
Collaborator Author

pocke commented Dec 25, 2016

Check the commit message. :-)

🙀 I fixed it.

@@ -1408,6 +1408,10 @@ Performance/RedundantSortBy:
Description: 'Use `sort` instead of `sort_by { |x| x }`.'
Enabled: true

Performance/RegexpMatch:
Description: 'Use `match?` instead of `Regexp#match`, `String#match` or `=~` when `MatchData` is not used.'
Copy link
Contributor

Choose a reason for hiding this comment

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

We might be better off not explicitly listing Regexp#match and String#match since match? is left more generic. This does not have reference to Symbol#match. Alternatively, we might want to be more explicit about saying use Regexp#match?, String#match?, or Symbol#match?.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's reasonable. 👍
I'll add to the description, and I'll update the spec.

module RuboCop
module Cop
module Performance
# In Ruby 2.4, `String#match?` and `Regexp#match?` have been added.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should reference Symbol#match? here.

'/re/.match(foo, 1)',
'/re/.match?(foo, 1)'],
['matching by =~`', '/re/ =~ foo', '/re/.match?(foo)'],
['matching by =~`', 'foo =~ /re/', 'foo.match?(/re/)']
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this include test cases for regex matching against symbols?

In Ruby 2.4, `String#match?` and `Regexp#match?` have been added.
https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/
https://bugs.ruby-lang.org/issues/8110

The methods are faster than `match`.
Because the methods avoid creating a `MatchData` object or saving backref.
So, when `MatchData` is not used, use `match?` instead of `match`.

This cop suggests using `match?` method.
@pocke
Copy link
Collaborator Author

pocke commented Dec 26, 2016

@rrosenblum Thanks for your suggestion!
I added descriotion and test forSymbol#match?.

@bbatsov bbatsov merged commit 864531f into rubocop:master Dec 26, 2016
@pocke pocke deleted the regexp-match branch December 26, 2016 13:17
@pocke pocke mentioned this pull request Jan 3, 2017
11 tasks
pocke added a commit to pocke/rubocop that referenced this pull request Jan 3, 2017
bbatsov pushed a commit that referenced this pull request Jan 3, 2017
koic pushed a commit to koic/rubocop-performance that referenced this pull request Oct 12, 2018
koic pushed a commit to rubocop/rubocop-rails that referenced this pull request Oct 26, 2018
patrickm53 pushed a commit to patrickm53/performance-develop-rubyonrails that referenced this pull request Sep 23, 2022
richardstewart0213 added a commit to richardstewart0213/performance-build-Performance-optimization-analysis- that referenced this pull request Nov 4, 2022
Cute0110 added a commit to Cute0110/Rubocop-Performance that referenced this pull request Sep 28, 2023
SerhiiMisiura added a commit to SerhiiMisiura/Rubocop-Performance that referenced this pull request Oct 5, 2023
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.

3 participants