Skip to content

Commit

Permalink
Merge pull request #321 from koic/fix_a_false_positive_for_performanc…
Browse files Browse the repository at this point in the history
…e_sum

Fix a false positive for `Performance/Sum`
  • Loading branch information
koic authored Dec 10, 2022
2 parents a5ebbc9 + febf747 commit dd328c0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_performance_sum.md
Original file line number Diff line number Diff line change
@@ -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][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/performance/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ module Performance
class Sum < Base
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.4

MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
MSG_IF_NO_INIT_VALUE =
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/performance/sum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit dd328c0

Please sign in to comment.