From febf74706329db579c2f6056b4b2880c3dc22a37 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 6 Dec 2022 11:28:56 +0900 Subject: [PATCH] Fix a false positive for `Performance/Sum` This PR fixes a false positive for `Performance/Sum` when using `TargetRubyVersion` is 2.3 or lower. Because `Array#sum` was introduced in Ruby 2.4. --- changelog/fix_a_false_positive_for_performance_sum.md | 1 + lib/rubocop/cop/performance/sum.rb | 3 +++ spec/rubocop/cop/performance/sum_spec.rb | 8 ++++++++ 3 files changed, 12 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_performance_sum.md diff --git a/changelog/fix_a_false_positive_for_performance_sum.md b/changelog/fix_a_false_positive_for_performance_sum.md new file mode 100644 index 0000000000..db0052fddf --- /dev/null +++ b/changelog/fix_a_false_positive_for_performance_sum.md @@ -0,0 +1 @@ +* [#321](https://github.com/rubocop/rubocop-performance/pull/321): Fix a false positive for `Performance/Sum` when using `TargetRubyVersion` is 2.3 or lower. ([@koic][]) diff --git a/lib/rubocop/cop/performance/sum.rb b/lib/rubocop/cop/performance/sum.rb index 10303a56b9..f81e00b7c4 100644 --- a/lib/rubocop/cop/performance/sum.rb +++ b/lib/rubocop/cop/performance/sum.rb @@ -70,6 +70,9 @@ module Performance class Sum < Base include RangeHelp extend AutoCorrector + extend TargetRubyVersion + + minimum_target_ruby_version 2.4 MSG = 'Use `%s` instead of `%s`.' MSG_IF_NO_INIT_VALUE = diff --git a/spec/rubocop/cop/performance/sum_spec.rb b/spec/rubocop/cop/performance/sum_spec.rb index e1c5bd9cb7..b771c2229f 100644 --- a/spec/rubocop/cop/performance/sum_spec.rb +++ b/spec/rubocop/cop/performance/sum_spec.rb @@ -304,6 +304,14 @@ array.sum RUBY end + + context 'when Ruby 2.3 or lower', :ruby23 do + it "does not register an offense when using `array.#{method}(10, :+)`" do + expect_no_offenses(<<~RUBY, method: method) + array.#{method}(10, :+) + RUBY + end + end end %i[map collect].each do |method|