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

gsub -> tr cop messes up escaped string #2154

Closed
cheerfulstoic opened this issue Aug 17, 2015 · 6 comments · Fixed by #2164
Closed

gsub -> tr cop messes up escaped string #2154

cheerfulstoic opened this issue Aug 17, 2015 · 6 comments · Fixed by #2164
Labels

Comments

@cheerfulstoic
Copy link

First off, thanks for rubocop!

So if I have a file like this:

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

rubocop (0.33.0 as well as GitHub master) transforms it to this:

"".tr('/', '\')

This isn't good because Ruby sees the second single quote as being escaped and there syntax errors.

Here's the rubocop output:

$ rubocop -aD test.rb
Inspecting 1 file
F

Offenses:

test.rb:1:4: C: [Corrected] Performance/StringReplacement: Use tr instead of gsub.
"".gsub('/', '\\')
   ^^^^^^^^^^^^^^^
test.rb:1:12: F: unterminated string meets end of file
"".tr('/', '\')
           ^

1 file inspected, 2 offenses detected, 1 offense corrected

rubocop -V:

$ rubocop -V
0.33.0 (using Parser 2.2.2.6, running on ruby 2.0.0 x86_64-darwin14.3.0)

You can also see my .rubocop.yml and .rubocop_todo.yml here:

https://gist.github.com/cheerfulstoic/062fa3b79e8397b82c19

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 18, 2015

/cc @rrosenblum

@bbatsov bbatsov added the bug label Aug 18, 2015
@rrosenblum
Copy link
Contributor

Thanks for the bug report. Escape characters tend to get nasty when dealing with auto-correct. I have a test case for "\n", but I guess that isn't good enough. I will look into a fix for this.

@cheerfulstoic
Copy link
Author

Awesome, thanks ;)

rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Aug 20, 2015
bbatsov added a commit that referenced this issue Aug 20, 2015
[Fix #2154] Fix auto-correct of StringReplacement with a backslash
@cheerfulstoic
Copy link
Author

🤘 Thanks

@aevernon
Copy link

I see the behavior below on master:

$ git branch -v
* master a584c06
$ cat test.rb
''.gsub('\\', '')
$ rubocop -aD test.rb
Inspecting 1 file
F

Offenses:


test.rb:1:4: C: [Corrected] Performance/StringReplacement: Use delete instead of gsub.
''.gsub('\\', '')
   ^^^^^^^^^^^^^^
test.rb:1:11: F: unterminated string meets end of file
''.delete('\')
          ^

1 file inspected, 3 offenses detected, 2 offenses corrected

@rrosenblum
Copy link
Contributor

Good catch. '\\' seems to be a special case compared to other escaped strings. I will find a fix.

rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Aug 30, 2015
bbatsov added a commit that referenced this issue Aug 31, 2015
[Fix #2154] Fix auto-correct of StringReplacement with a backslash again
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.

4 participants