Skip to content

Commit

Permalink
Merge pull request #418 from koic/support_optimized_string_dup_for_pe…
Browse files Browse the repository at this point in the history
…rformance_unfreeze_string

[Fix #384] Support optimized `String#dup` for `Performance/UnfreezeString`
  • Loading branch information
koic authored Dec 7, 2023
2 parents 76911d0 + 7ac5b60 commit bf85c6d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" \
-e "/gem 'rubocop-rspec',/d" -i Gemfile
cat << EOF > Gemfile.local
gem 'rubocop', '1.30.0' # Specify the oldest supported RuboCop version
gem 'rubocop', '1.48.1' # Specify the oldest supported RuboCop version
EOF
- name: set up Ruby
uses: ruby/setup-ruby@v1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#384](https://github.com/rubocop/rubocop-performance/issues/384): Support optimized `String#dup` for `Performance/UnfreezeString` when Ruby 3.3+. ([@koic][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/performance/unfreeze_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module Performance
#
# @example
# # bad
# ''.dup
# "something".dup
# ''.dup # when Ruby 3.2 or lower
# "something".dup # when Ruby 3.2 or lower
# String.new
# String.new('')
# String.new('something')
Expand Down Expand Up @@ -45,7 +45,7 @@ class UnfreezeString < Base
PATTERN

def on_send(node)
return unless dup_string?(node) || string_new?(node)
return unless (dup_string?(node) && target_ruby_version <= 3.2) || string_new?(node)

add_offense(node) do |corrector|
string_value = "+#{string_value(node)}"
Expand Down
2 changes: 1 addition & 1 deletion rubocop-performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ Gem::Specification.new do |s|
'rubygems_mfa_required' => 'true'
}

s.add_runtime_dependency('rubocop', '>= 1.30.0', '< 2.0')
s.add_runtime_dependency('rubocop', '>= 1.48.1', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.30.0', '< 2.0')
end
26 changes: 18 additions & 8 deletions spec/rubocop/cop/performance/unfreeze_string_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Performance::UnfreezeString, :config do
it 'registers an offense and corrects for an empty string with `.dup`' do
expect_offense(<<~RUBY)
"".dup
^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY
context 'when Ruby <= 3.2', :ruby32 do
it 'registers an offense and corrects for an empty string with `.dup`' do
expect_offense(<<~RUBY)
"".dup
^^^^^^ Use unary plus to get an unfrozen string literal.
RUBY

expect_correction(<<~RUBY)
+""
RUBY
expect_correction(<<~RUBY)
+""
RUBY
end
end

context 'when Ruby >= 3.3', :ruby33 do
it 'does not register an offense and corrects for an empty string with `.dup`' do
expect_no_offenses(<<~RUBY)
"".dup
RUBY
end
end

it 'registers an offense and corrects for a string with `.dup`' do
Expand Down

0 comments on commit bf85c6d

Please sign in to comment.