You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
triggers the Style/RedundantRegexpArgument cop which is fine.
But the autocorrect turns the code into
defcleaner(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
defcleaner(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
defcleaner(dirty)dirty.gsub(/"',/,'')end
rubocop -A
turns it into this:
defcleaner(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
…RegexpArgument`
Fixesrubocop#11981.
This PR fixes an incorrect autocorrect for `Style/RedundantRegexpArgument`
when using double quote and single quote characters.
Problem
The following substitution command:
triggers the
Style/RedundantRegexpArgument
cop which is fine.But the autocorrect turns the code into
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
Expected behavior
Given the code
Rubocop should complain about the
Style/RedundantRegexpArgument
But either the
autocorrect
should not happen or it should autocorrect toActual behavior
Given this
rubocop -A
turns it into this:
which is invalid syntax because for the misaligned single quotes.
Debug output
Steps to reproduce the problem
Write a file
Run
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
The text was updated successfully, but these errors were encountered: