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

Replacement of %q when escape characters are present can break code #3300

Closed
tonymarklove opened this issue Jul 8, 2016 · 0 comments
Closed

Comments

@tonymarklove
Copy link

tonymarklove commented Jul 8, 2016

The rule that replaces %q with a double quote string literal is not correct in the presence of both an escaped backslash and an escaped single quote.

Our real use case was to replace 'hello' (quotes are part of the string) with \'hello\'.


Expected behavior

No change.

Actual behavior

diff --git a/example.rb b/example.rb
index ef2b914..380c92a 100644
--- a/example.rb
+++ b/example.rb
@@ -7,7 +7,7 @@ class TestSample < Test::Unit::TestCase
     target = 'foobar'
     expected = "\\'bar"
     assert do
-      target.gsub('foo', %q(\\\')) == expected
+      target.gsub('foo', "\\\'") == expected
     end
   end
 end

Steps to reproduce the problem

example.rb

# frozen_string_literal: true
require 'test/unit'

# comment
class TestSample < Test::Unit::TestCase
  test 'replace' do
    target = 'foobar'
    expected = "\\'bar"
    assert do
      target.gsub('foo', %q(\\\')) == expected
    end
  end
end
$ ruby example.rb
Loaded suite example
Started
.

Finished in 0.000961 seconds.
---------------------------------------------------------------------------------------------------------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
---------------------------------------------------------------------------------------------------------
$ rubocop -a example.rb
Inspecting 1 file
C

Offenses:

example.rb:10:26: C: [Corrected] Use %q only for strings that contain both single quotes and double quotes.
      target.gsub('foo', %q(\\\')) == expected
                         ^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
ruby example.rb
Loaded suite example
Started
F
=========================================================================================================
Failure:
        target.gsub('foo', "\\\'") == expected
        |      |                   |  |
        |      |                   |  "\\'bar"
        |      |                   false
        |      "barbar"
        "foobar"
test: replace(TestSample)
/Users/tony/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/power_assert-0.2.6/lib/power_assert.rb:36:in `start'
example.rb:9:in `block in <class:TestSample>'
      6:   test 'replace' do
      7:     target = 'foobar'
      8:     expected = "\\'bar"
  =>  9:     assert do
     10:       target.gsub('foo', "\\\'") == expected
     11:     end
     12:   end
=========================================================================================================


Finished in 0.005932 seconds.
---------------------------------------------------------------------------------------------------------
1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
---------------------------------------------------------------------------------------------------------

RuboCop version

$ rubocop -V
0.41.1 (using Parser 2.3.1.2, running on ruby 2.3.1 x86_64-darwin15)
@tonymarklove tonymarklove changed the title Replacement of %q when escape characters are present can break code Replacement of %q when escape characters are present can break code Jul 8, 2016
owst pushed a commit to owst/rubocop that referenced this issue Jul 19, 2016
Neodelf pushed a commit to Neodelf/rubocop that referenced this issue Oct 15, 2016
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