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

False positives for match in RegexpMatch #48

Closed
dduugg opened this issue May 3, 2019 · 4 comments · Fixed by #49
Closed

False positives for match in RegexpMatch #48

dduugg opened this issue May 3, 2019 · 4 comments · Fixed by #49

Comments

@dduugg
Copy link
Contributor

dduugg commented May 3, 2019

The RegexpMatch is a bit too eager in flagging usage of match. Here's a simple example that is flagged when extending Rubocop's own API:

class MyCop < Cop
  KERNEL_PATTERN = RuboCop::NodePattern.new('(const nil? :Kernel)')
  def on_send(node)
    if KERNEL_PATTERN.match(node)
      # do stuff
    end
  end
end

I would suggest making the match_method? logic more selective, and only flag usage by receiver types known to have a regexp matching match method. At risk of getting too into implementation details, rather than:

def_node_matcher :match_method?, <<-PATTERN
  {
    (send _recv :match _)
    (send _recv :match _ (int ...))
  }
PATTERN

RegexpMatch could instead do:

def_node_matcher :match_method?, <<-PATTERN
  {
    (send {regexp str sym} :match _)
    (send {regexp str sym} :match _ (int ...))
  }
PATTERN

I'm happy to open a PR if this is agreeable.

Expected behavior

The snippet above should not trigger a Performance/RegexpMatch violation

Actual behavior

Offenses:

repro.rb:4:8: C: Performance/RegexpMatch: Use match? instead of match when MatchData is not used.
    if KERNEL_PATTERN.match(node)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Steps to reproduce the problem

rubocop file.rb --only Performance/RegexpMatch with the above snippet saved in file.rb

RuboCop version

0.68.0 (using Parser 2.6.0.0, running on ruby 2.4.5 x86_64-darwin17)
@bquorning
Copy link
Contributor

I believe #47 would fix this issue?

@dduugg
Copy link
Contributor Author

dduugg commented May 4, 2019

I think that's a different issue. Let me put together a PR with a failing test and a suggested fix, it might take a day or three though.

@koic
Copy link
Member

koic commented May 4, 2019

Yes. This is different from #47.
@pocke You are the original author of Performance/RegexpMatch cop. Do you have an opinion on this proposal?

dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 4, 2019
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 4, 2019
dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 4, 2019
@pocke
Copy link
Contributor

pocke commented May 6, 2019

I agree with this change. Thank you!

dduugg added a commit to dduugg/rubocop-performance that referenced this issue May 9, 2019
@koic koic closed this as completed in #49 May 10, 2019
koic added a commit that referenced this issue May 10, 2019
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 a pull request may close this issue.

4 participants