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/RedundantRegexpArgument: Autocorrect can lead to invalid ruby syntax with single quotes #11981

Closed
bunnymatic opened this issue Jun 25, 2023 · 0 comments · Fixed by #11982
Labels

Comments

@bunnymatic
Copy link

Problem

The following substitution command:

def cleaner(dirty)
  dirty.gsub(/"',/, '')
end

triggers the Style/RedundantRegexpArgument cop which is fine.

But the autocorrect turns the code into

def cleaner(dirty)
  dirty.gsub('"',', '')
end

which causes a Lint/Syntax: unterminated string meets end of file because the contents of the regex are no longer correctly escaped.

To make it work with quotes instead of regex, the corrected line should read

def cleaner(dirty)
   dirty.gsub('"\',', '')
end

Expected behavior

Given the code

dirty.gsub(/"',/, '')`

Rubocop should complain about the Style/RedundantRegexpArgument
But either the autocorrect should not happen or it should autocorrect to

dirty.gsub('"\',', '')

Actual behavior

Given this

def cleaner(dirty)
  dirty.gsub(/"',/, '')
end

rubocop -A

turns it into this:

def cleaner(dirty)
  dirty.gsub('"',', '')
end

which is invalid syntax because for the misaligned single quotes.

Debug output

$ be rubocop --debug -A redundant_regex_arg_example.rb

For /Users/jon/projects/whatever: configuration from /Users/jon/projects/whatever/.rubocop.yml
configuration from /Users/jon/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.18.0/config/default.yml
configuration from /Users/jon/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.18.0/config/default.yml
Default configuration from /Users/jon/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-1.53.0/config/default.yml
configuration from /Users/jon/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-rails-2.20.2/config/default.yml
configuration from /Users/jon/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/rubocop-rails-2.20.2/config/default.yml
Inheriting configuration from /Users/jon/projects/whatever/.rubocop.todo.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/jon/projects/whatever/redundant_regex_arg_example.rb
Loading cache from /Users/jon/.cache/rubocop_cache/8449699e90a8eb689085bbe8d22aeb5d4118dfa3/4d9639fac8698c2fd87b93646cd9846b98daa958/65733ef997f91c839b5ca0b49385d02191f50232
F

Offenses:

redundant_regex_arg_example.rb:2:14: C: [Corrected] Style/RedundantRegexpArgument: Use string '"',' as argument instead of regexp /"',/.
  dirty.gsub(/"',/, '')
             ^^^^^
redundant_regex_arg_example.rb:2:22: F: Lint/Syntax: unterminated string meets end of file
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
  dirty.gsub('"',', '')
                     ^

1 file inspected, 2 offenses detected, 1 offense corrected
Finished in 0.564159999601543 seconds

Steps to reproduce the problem

Write a file

# redundant_regex_arg_example.rb                
def cleaner(dirty)
  dirty.gsub(/"',/, '')
end

Run

rubocop -A redundant_regex_arg_example.rb

Notice how you end up with a syntax error in the file after the autocorrect has happened.

RuboCop version

bundle exec rubocop -V
1.53.0 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 2.7.6) [x86_64-darwin21]
  - rubocop-performance 1.18.0
  - rubocop-rails 2.20.2
@koic koic added the bug label Jun 26, 2023
koic added a commit to koic/rubocop that referenced this issue Jun 26, 2023
…RegexpArgument`

Fixes rubocop#11981.

This PR fixes an incorrect autocorrect for `Style/RedundantRegexpArgument`
when using double quote and single quote characters.
bbatsov pushed a commit that referenced this issue Jun 26, 2023
…rgument`

Fixes #11981.

This PR fixes an incorrect autocorrect for `Style/RedundantRegexpArgument`
when using double quote and single quote characters.
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