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

[Fix #384] Support optimized String#dup for Performance/UnfreezeString #418

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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