From e6e20e3030863614ae8405fdd687dd33c5d0e5b2 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sat, 16 Sep 2023 16:49:01 +0200 Subject: [PATCH] Set target version for `Performance/UnfreezeString` 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 --- changelog/fix_target_version_for_unfreeze_string_cop.md | 1 + lib/rubocop/cop/performance/unfreeze_string.rb | 3 +++ spec/rubocop/cop/performance/unfreeze_string_spec.rb | 8 ++++++++ 3 files changed, 12 insertions(+) create mode 100644 changelog/fix_target_version_for_unfreeze_string_cop.md 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