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 a false negartive for Performance/RegexpMatch #47

Merged
merged 1 commit into from
May 4, 2019

Conversation

koic
Copy link
Member

@koic koic commented May 2, 2019

Follow up rubocop/rubocop#6965.

This PR fixes a false negartive for Performance/RegexpMatch when using RuboCop 0.68 or higher.

# example.rb
def foo
  if /re/.match(foo, 1)
    do_something
  end
end

RuboCop 0.67

RuboCop 0.67 warns.

% rubocop _0.67.2_ --only Performance/RegexpMatch
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: Performance/RegexpMatch: Use match? instead of match
when MatchData is not used.
  if /re/.match(foo, 1)
     ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

RuboCop 0.68 (Before)

RuboCop 0.68 does not warn. This is a false negative.

% rubocop _0.68.1_ --only Performance/RegexpMatch --require
rubocop-performance
Inspecting 1 file
.

1 file inspected, no offenses detected

RuboCop 0.68 (After)

This PR fixes a false negative.

% rubocop --only Performance/RegexpMatch --require rubocop-performance
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: Performance/RegexpMatch: Use match? instead of match
when MatchData is not used.
  if /re/.match(foo, 1)
     ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

This fix uses the following <> node pattern notation introduced in RuboCop 0.68, so it supports Ruby version 0.68 or higher.
rubocop/rubocop#6965


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 bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic mentioned this pull request May 2, 2019
8 tasks
@koic
Copy link
Member Author

koic commented May 2, 2019

I will release RuboCop Performance 1.2.0 after being merged with this PR.

@bquorning
Copy link
Contributor

I think we should add a spec example that would catch this.

@koic
Copy link
Member Author

koic commented May 3, 2019

My description was lacking. I noticed that existing tests are falling.
https://circleci.com/gh/rubocop-hq/rubocop-performance/419

So I think that there is no need to add a test to this PR because they are covered by existing tests.

I think that it is good to add a test of NodePattern to RuboCop Core if adding a test.

Follow up rubocop/rubocop#6965.

This PR fixes a false negartive for `Performance/RegexpMatch`
when using RuboCop 0.68 or higher.

```ruby
# example.rb
def foo
  if /re/.match(foo, 1)
    do_something
  end
end
```

## RuboCop 0.67

RuboCop 0.67 warns.

```console
% rubocop _0.67.2_ --only Performance/RegexpMatch
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: Performance/RegexpMatch: Use match? instead of match
when MatchData is not used.
  if /re/.match(foo, 1)
       ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

## RuboCop 0.68 (Before)

RuboCop 0.68 does not warn. This is a false negative.

```console
% rubocop _0.68.1_ --only Performance/RegexpMatch --require
rubocop-performance
Inspecting 1 file
.

1 file inspected, no offenses detected
```

## RuboCop 0.68 (After)

This PR fixes a false negative.

```console
% rubocop --only Performance/RegexpMatch --require rubocop-performance
Inspecting 1 file
C

Offenses:

example.rb:2:6: C: Performance/RegexpMatch: Use match? instead of match
when MatchData is not used.
  if /re/.match(foo, 1)
       ^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

This fix uses the following `<>` node pattern notation introduced in
RuboCop 0.68, so it supports Ruby version 0.68 or higher.
rubocop/rubocop#6965
@koic koic force-pushed the fix_a_false_negative_for_regexp_match branch from 6a5de0c to 25e8894 Compare May 3, 2019 13:37
@koic koic merged commit 37d3d30 into rubocop:master May 4, 2019
@koic koic deleted the fix_a_false_negative_for_regexp_match branch May 4, 2019 11:44
koic added a commit to koic/rubocop that referenced this pull request May 4, 2019
RuboCop Performance 1.2.0 has been released.
https://rubygems.org/gems/rubocop-performance/versions/1.2.0

This version includes the following fix false negative to
`Performance/RegexpMatch` cop.
rubocop/rubocop-performance#47
natesholland added a commit to natesholland/codeclimate-rubocop that referenced this pull request May 13, 2019
@koic recommended we bump to a newer version. The fix we are taking in
can be found here: rubocop/rubocop-performance#47
koic added a commit to koic/rubocop-performance that referenced this pull request Jun 6, 2019
Follow up rubocop/rubocop#7081.

This PR fixes a false negartive for `Performance/RegexpMatch` when using
RuboCop 0.71 or higher.

This causes CI to fail.
https://circleci.com/gh/rubocop-hq/rubocop-performance/588

This is a regression similar to rubocop#47. This PR tries to fix it using
a node pattern that has been used for a long time.
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

2 participants