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

Style/RegexpLiteral not accounting for ambigous first argument #9874

Closed
mojavelinux opened this issue Jun 12, 2021 · 0 comments · Fixed by #9878
Closed

Style/RegexpLiteral not accounting for ambigous first argument #9874

mojavelinux opened this issue Jun 12, 2021 · 0 comments · Fixed by #9878
Labels

Comments

@mojavelinux
Copy link

If we enable the Style/RegexpLiteral rule together with Style/MethodCallWithArgsParentheses using the EnforcedStyle omit_parentheses, RuboCop incorrectly recommends using // around a regular expression when the %r style is needed to avoid an ambiguous first argument.

Expected behavior

Consider the following rules:

Style/MethodCallWithArgsParentheses:
  Enabled: true
  EnforcedStyle: omit_parentheses
Style/RegexpLiteral:
  Enabled: false

Now consider the following code:

str = 'foo'
str = str.gsub %r/foo/, 'bar'
puts str

We expect RuboCop to be fine with this code because %r is needed to avoid an ambiguous first argument warning from Ruby.

Actual behavior

RuboCop reports the following problem:

C: [Correctable] Style/RegexpLiteral: Use // around regular expression.

If we take away the %r, Ruby reports the following warning:

warning: ambiguous first argument; put parentheses or a space even after `/' operator

I think RuboCop should recognize this situation and allow for the use of %r in this case.

RuboCop version

1.16.0 (using Parser 3.0.1.1, rubocop-ast 1.7.0, running on ruby 2.7.2 x86_64-linux)
  - rubocop-rake 0.5.1
  - rubocop-rspec 2.4.0
@bbatsov bbatsov added the bug label Jun 12, 2021
koic added a commit to koic/rubocop that referenced this issue Jun 15, 2021
Fixes rubocop#9874.

This PR fixes a false positive for `Style/RegexpLiteral`
when using `%r` regexp literal with `EnforcedStyle: omit_parentheses` of
`Style/MethodCallWithArgsParentheses`.
koic added a commit that referenced this issue Jun 15, 2021
…p_literal

[Fix #9874] Fix a false positive for `Style/RegexpLiteral`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants