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

Syntax error auto-correcting Performance/RegexpMatch #55

Closed
dduugg opened this issue May 13, 2019 · 1 comment · Fixed by #109
Closed

Syntax error auto-correcting Performance/RegexpMatch #55

dduugg opened this issue May 13, 2019 · 1 comment · Fixed by #109
Labels
bug Something isn't working

Comments

@dduugg
Copy link
Contributor

dduugg commented May 13, 2019

I'm getting a syntax error auto-correcting a Performance/RegexpMatch violation in the following method definition:

def foo(id)
  if id.=~(/\./)
    'yes'
  else
    'no'
  end
end

Expected behavior

I expect this to autocorrect to:

def foo(id)
  if /\./.match?(id)
    'yes'
  else
    'no'
  end
end

Actual behavior

The autocorrection has an extra ), leading to a syntax error:

def foo(id)
  if /\./.match?(id))
    'yes'
  else
    'no'
  end
end

Steps to reproduce the problem

Put the above snippet into a foo.rb file, then:

$ bundle exec rubocop foo.rb -a
Inspecting 1 file
E

Offenses:
foo.rb:4:6: C: [Corrected] Performance/RegexpMatch: Use match? instead of =~ when MatchData is not used.
  if id.=~(/\./)
     ^^^^^^^^^^^
foo.rb:7:21: E: Lint/Syntax: unexpected token tRPAREN
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
  if /\./.match?(id))
                    ^
foo.rb:9:3: W: Lint/Syntax: else without rescue is useless
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
  else
  ^^^^
foo.rb:12:1: E: Lint/Syntax: unexpected token kEND
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
end
^^^

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:

$ bundle exec rubocop -V
0.69.0 (using Parser 2.6.2.0, running on ruby 2.4.5 x86_64-darwin17)

(rubocop-performance version is 1.3.0)

@tejasbubane
Copy link
Contributor

@koic I was trying to fix this error and have a doubt. Both FOO =~ re and FOO.=~(re) produce the exact same AST:

 s(:send,
  s(:const, nil, :FOO), :=~,
  s(:send, nil, :re))

How can we differentiate between the two? without falling back to source string that is.

@koic koic added the bug Something isn't working label May 9, 2020
koic added a commit to koic/rubocop-performance that referenced this issue May 16, 2020
…Match`

Fixes rubocop#55.

This PR fixes an incorrect autocorrect for `Performance/RegexpMatch` cop
when using `str.=~(/regexp/)`.
@koic koic closed this as completed in #109 May 18, 2020
koic added a commit that referenced this issue May 18, 2020
…rmance_regexp_match

[Fix #55] Fix an incorrect autocorrect for `Performance/RegexpMatch`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants