KEMBAR78
Style/RedundantRegexpArgument auto-correct to wrong unterminated string from a single-quote regexp `/\'/` · Issue #14543 · rubocop/rubocop · GitHub
Skip to content

Style/RedundantRegexpArgument auto-correct to wrong unterminated string from a single-quote regexp /\'/ #14543

@ngoto

Description

@ngoto

After auto-correct the following script, syntax error occurred.

source code:

require 'strscan'
module AAA
  def check_single_quote(str)
    ss = StringScanner.new(str)
    ss.scan(/\'/)
  end
end

.rubocop.yml:

AllCops:
  NewCops: enable

Expected behavior

Auto-corrected script runs without error.

Actual behavior

The above script is auto-corrected to the following script:

require 'strscan'
module AAA
  def check_single_quote(str)
    ss = StringScanner.new(str)
    ss.scan('\\'')
  end
end

Running the auto-corrected script, the following error occurred.

$ ruby tmp09.rb
tmp09.rb: --> tmp09.rb

Unmatched `(', missing `)' ?

  2  module AAA
  3    def check_single_quote(str)
> 4      ss = StringScanner.new(str)
> 5      ss.scan('\\'')
  6    end
  7  end

tmp09.rb:5: syntax errors found (SyntaxError)
  3 |   def check_single_quote(str)
  4 |     ss = StringScanner.new(str)
> 5 |     ss.scan('\\'')
    |                 ^ unterminated string meets end of file
  6 |   end
  7 | end
> 8 |
    | ^ expected an `end` to close the `module` statement

    | ^ expected an `end` to close the `def` statement

    | ^ unexpected end-of-input, assuming it is closing the parent top level context

    | ^ unexpected end-of-input; expected a `)` to close the arguments

$

Steps to reproduce the problem

RuboCop log:

$ rubocop -d -a tmp09.rb
For /XXXX/202509/.rubocop.yml
Default configuration from /YYYY/amd64/3.4.6/lib/ruby/gems/3.4.0/gems/rubocop-1.80.2/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /XXXX/202509/tmp09.rb
Loading cache from /XXXX/.cache/rubocop_cache/178c5ca2f83a8cb4cb3064ce06de0eff8fa19ca9/6d7a3b621ca1730e04accd938619e4bdab66cfb1/fe41aad91eefe86e96fb2109a56bdc0594a75e65
F

Offenses:

tmp09.rb:5:13: C: [Corrected] Style/RedundantRegexpArgument: Use string '\\'' as argument instead of regexp /\'/.
    ss.scan(/\'/)
            ^^^^
tmp09.rb:5:14: C: [Corrected] Style/RedundantRegexpEscape: Redundant escape inside regexp literal
    ss.scan(/\'/)
             ^^
tmp09.rb:5:17: F: Lint/Syntax: unterminated string meets end of file
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
    ss.scan('\\'')
                ^

1 file inspected, 3 offenses detected, 2 offenses corrected
Finished in 0.32895121996989474 seconds
$ 

RuboCop version

$ rubocop -V
1.80.2 (using Parser 3.3.9.0, rubocop-ast 1.46.0, analyzing as Ruby 2.7, running on ruby 3.4.6) [x86_64-linux]
$ ruby -v
ruby 3.4.6 (2025-09-16 revision dbd83256b1) +PRISM [x86_64-linux]
$ cat .rubocop.yml
AllCops:
  NewCops: enable
$

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions