Skip to content

Commit

Permalink
Set target version for Performance/UnfreezeString
Browse files Browse the repository at this point in the history
As stated in the class documentation, unary plus operator is available
from Ruby 2.3.

This fix prevent RuboCop Performance to raise an offense when configured
to support Ruby 2.2
  • Loading branch information
tagliala committed Sep 16, 2023
1 parent 0781feb commit e6e20e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_target_version_for_unfreeze_string_cop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#373](https://github.com/rubocop/rubocop-performance/pull/373): Set target version for `Performance/UnfreezeString`. ([@tagliala][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/performance/unfreeze_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module Performance
# +''
class UnfreezeString < Base
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.3

MSG = 'Use unary plus to get an unfrozen string literal.'
RESTRICT_ON_SEND = %i[dup new].freeze
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/performance/unfreeze_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,12 @@
String.new(capacity: 100)
RUBY
end

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

0 comments on commit e6e20e3

Please sign in to comment.