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/RedundantRegexpEscape flags escaped delimeters in %r regexps #8131

Closed
pyzlnar opened this issue Jun 10, 2020 · 0 comments
Closed

Style/RedundantRegexpEscape flags escaped delimeters in %r regexps #8131

pyzlnar opened this issue Jun 10, 2020 · 0 comments

Comments

@pyzlnar
Copy link

pyzlnar commented Jun 10, 2020

Expected behavior

The Style/RedundantRegexpEscape cop should not flag or fix escaped characters when they are being used as delimiters in a %r type regexp.

Actual behavior

The Style/RedundantRegexpEscape cop flags and fixes escaped characters when they are being used as delimiters in a %r type regexp. This causes a Syntax Error under several ruby versions.

Steps to reproduce the problem

Take this regexp matching for / or } characters:

%r{[/\}]}

Currently the cop will register this as an offense, and if run with autocorrect creates a syntax error.

$ rubocop test_file.rb -a --only Style/RedundantRegexpEscape
Inspecting 1 file
E

Offenses:

test_file.rb:1:1: E: Lint/Syntax: premature end of char-class: /[\//
(Using Ruby 2.6 parser; configure using TargetRubyVersion parameter, under AllCops)
%r{[/}]}
^^^^^^
test_file.rb:1:6: C: [Corrected] Style/RedundantRegexpEscape: Redundant escape inside regexp literal
%r{[/\}]}
     ^^

1 file inspected, 2 offenses detected, 1 offense corrected

Keep in mind that this applies for any delimiter, whether it be braces { } or other less common delimiters.

# Other contrived examples

# Opening Brace
%r{[/\{]}

# Closing Parenthesis
%r([/\)])

# Pipe
%r|[/\|]|

# Outside brackets
%r-a\-b-

# etc...
%r~[/\~]~
rubocop test_file.rb --only Style/RedundantRegexpEscape
Inspecting 1 file
C

Offenses:

test_file.rb:2:6: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
%r{[/\{]}
     ^^
test_file.rb:5:6: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
%r([/\)])
     ^^
test_file.rb:8:6: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
%r|[/\|]|
     ^^
test_file.rb:11:5: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
%r-a\-b-
    ^^
test_file.rb:14:6: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
%r~[/\~]~
     ^^

1 file inspected, 5 offenses detected

RuboCop version

$ rubocop -V
0.85.1 (using Parser 2.7.1.3, rubocop-ast 0.0.3, running on ruby 2.6.3 x86_64-linux)
owst added a commit to owst/rubocop that referenced this issue Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant