diff --git a/changelog/fix_target_version_for_unfreeze_string_cop.md b/changelog/fix_target_version_for_unfreeze_string_cop.md new file mode 100644 index 0000000000..c9573238d6 --- /dev/null +++ b/changelog/fix_target_version_for_unfreeze_string_cop.md @@ -0,0 +1 @@ +* [#373](https://github.com/rubocop/rubocop-performance/pull/373): Set target version for `Performance/UnfreezeString`. ([@tagliala][]) diff --git a/lib/rubocop/cop/performance/unfreeze_string.rb b/lib/rubocop/cop/performance/unfreeze_string.rb index 7860c269c7..1b101dc8c8 100644 --- a/lib/rubocop/cop/performance/unfreeze_string.rb +++ b/lib/rubocop/cop/performance/unfreeze_string.rb @@ -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 diff --git a/spec/rubocop/cop/performance/unfreeze_string_spec.rb b/spec/rubocop/cop/performance/unfreeze_string_spec.rb index 5db13a118d..d8e27b6151 100644 --- a/spec/rubocop/cop/performance/unfreeze_string_spec.rb +++ b/spec/rubocop/cop/performance/unfreeze_string_spec.rb @@ -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