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

Performance/StringReplacement autocorrect breaks /\R/ #3272

Closed
sanemat opened this issue Jul 1, 2016 · 0 comments
Closed

Performance/StringReplacement autocorrect breaks /\R/ #3272

sanemat opened this issue Jul 1, 2016 · 0 comments

Comments

@sanemat
Copy link

sanemat commented Jul 1, 2016


Expected behavior

no change

Actual behavior

diff --git a/example.rb b/example.rb
index 76f186f..d0847bb 100644
--- a/example.rb
+++ b/example.rb
@@ -7,7 +7,7 @@ class TestSample < Test::Unit::TestCase
     target = "This\nis\r\nReds."
     expected = 'This|is|Reds.'
     assert do
-      target.gsub(/\R/, '|') == expected
+      target.tr('R', '|') == 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 = "This\nis\r\nReds."
    expected = 'This|is|Reds.'
    assert do
      target.gsub(/\R/, '|') == expected
    end
  end
end
$ bundle exec ruby example.rb 
Loaded suite example
Started
.

Finished in 0.00045 seconds.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2222.22 tests/s, 2222.22 assertions/s
$ bundle exec rubocop -a example.rb
Inspecting 1 file
C

Offenses:

example.rb:10:14: C: [Corrected] Use tr instead of gsub.
      target.gsub(/\R/, '|') == expected
             ^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

then

example.rb

# frozen_string_literal: true
require 'test/unit'

# comment
class TestSample < Test::Unit::TestCase
  test 'replace' do
    target = "This\nis\r\nReds."
    expected = 'This|is|Reds.'
    assert do
      target.tr('R', '|') == expected
    end
  end
end
$ bundle exec ruby example.rb 
Loaded suite example
Started
F
==================================================================================================================================================================================
Failure:
        target.tr('R', '|') == expected
        |      |            |  |
        |      |            |  "This|is|Reds."
        |      |            false
        |      "This\nis\r\n|eds."
        "This\nis\r\nReds."
test: replace(TestSample)
/Users/murahashi.kenichi/temp/foog/vendor/bundle/ruby/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 = "This\nis\r\nReds."
      8:     expected = 'This|is|Reds.'
  =>  9:     assert do
     10:       target.tr('R', '|') == expected
     11:     end
     12:   end
==================================================================================================================================================================================


Finished in 0.008325 seconds.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
120.12 tests/s, 120.12 assertions/s

RuboCop version

Include the output of rubocop -V:

$ bundle exec rubocop -V
0.41.1 (using Parser 2.3.1.2, running on ruby 2.3.1 x86_64-darwin15)

Additional Info

.rubocop.yml

AllCops:
  TargetRubyVersion: 2.3

Gemfile

source "https://rubygems.org"
gem "rubocop"
gem "test-unit"
@sanemat sanemat changed the title Performance/StringReplacement autocorrect breaks code Performance/StringReplacement autocorrect breaks /\R/ Jul 1, 2016
pocke added a commit to pocke/rubocop that referenced this issue Jul 6, 2016
@bbatsov bbatsov closed this as completed in 6a1fc93 Jul 6, 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