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

RedundantRegexpEscape tells me backslashes are redundant even though they are not #8456

Closed
betesh opened this issue Aug 5, 2020 · 1 comment

Comments

@betesh
Copy link
Contributor

betesh commented Aug 5, 2020

Regexp literals that contain [^:] confuse the RedundantRegexpEscape cop into thinking that later backslashes are redundant.


Expected behavior

Rubocop should not identify any offenses in the example below. It should recognize that the backslashes are meaningful in context.

Actual behavior

Rubocop reports that backslashes are redundant even though they are not.

Steps to reproduce the problem

$ cat test.rb 
# These 2 have different results, so the backslash 
# before the dot is not redundant
puts /[^:]+./.match?('ab')
puts /[^:]+\./.match?('ab')

# These 2 have different results, so the backslashes 
# before the opening and closing parentheses are not redundant
puts /[^:]\(u\)/.match?(':(u)')
puts /[^:](u)/.match?(':(u)')

$ ruby test.rb 
true
false
false
true

$ rubocop --only Style/RedundantRegexpEscape
Inspecting 1 file
C

Offenses:

test.rb:4:12: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
puts /[^:]+\./.match?('ab')
           ^^
test.rb:8:11: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
puts /[^:]\(u\)/.match?(':(u)')
          ^^
test.rb:8:14: C: Style/RedundantRegexpEscape: Redundant escape inside regexp literal
puts /[^:]\(u\)/.match?(':(u)')
             ^^

1 file inspected, 3 offenses detected

RuboCop version

$ rubocop -V
0.88.0 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.5.0 x86_64-linux)
@betesh
Copy link
Contributor Author

betesh commented Aug 5, 2020

This is fixed in master. Probably duplicates #8335

@betesh betesh closed this as completed Aug 5, 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