From b59c2606cbf41de100d717f7f2b0c56b80662c1a Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sat, 16 Oct 2021 11:00:22 +0200 Subject: [PATCH] Fix error in Performance/Sum when method has no brackets This fixes an issue where code like the following would cause an error in the Performance/Sum cop: arr.inject :+ --- CHANGELOG.md | 1 + ...fix_error_in_performancesum_when_method.md | 1 + lib/rubocop/cop/performance/sum.rb | 2 +- spec/rubocop/cop/performance/sum_spec.rb | 31 +++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_fix_error_in_performancesum_when_method.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 610ad42679..b5dadb9233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -296,3 +296,4 @@ [@dvandersluis]: https://github.com/dvandersluis [@ghiculescu]: https://github.com/ghiculescu [@mfbmina]: https://github.com/mfbmina +[@mvz]: https://github.com/mvz diff --git a/changelog/fix_fix_error_in_performancesum_when_method.md b/changelog/fix_fix_error_in_performancesum_when_method.md new file mode 100644 index 0000000000..587e6c273a --- /dev/null +++ b/changelog/fix_fix_error_in_performancesum_when_method.md @@ -0,0 +1 @@ +* [#264](https://github.com/rubocop/rubocop-performance/pull/264): Fix error in Performance/Sum when method has no brackets. ([@mvz][]) diff --git a/lib/rubocop/cop/performance/sum.rb b/lib/rubocop/cop/performance/sum.rb index df336773b3..30067a4c09 100644 --- a/lib/rubocop/cop/performance/sum.rb +++ b/lib/rubocop/cop/performance/sum.rb @@ -156,7 +156,7 @@ def autocorrect_sum_map(corrector, sum, map, init) end def sum_method_range(node) - range_between(node.loc.selector.begin_pos, node.loc.end.end_pos) + range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos) end def sum_map_range(map, sum) diff --git a/spec/rubocop/cop/performance/sum_spec.rb b/spec/rubocop/cop/performance/sum_spec.rb index aa4aeb6824..a3267c543e 100644 --- a/spec/rubocop/cop/performance/sum_spec.rb +++ b/spec/rubocop/cop/performance/sum_spec.rb @@ -46,6 +46,17 @@ RUBY end + it "registers an offense and corrects when using `array.#{method} 0, :+`" do + expect_offense(<<~RUBY, method: method) + array.#{method} 0, :+ + ^{method}^^^^^^ Use `sum` instead of `#{method}(0, :+)`. + RUBY + + expect_correction(<<~RUBY) + array.sum + RUBY + end + it "registers an offense and corrects when using `array.#{method}(0.0, :+)`" do expect_offense(<<~RUBY, method: method) array.#{method}(0.0, :+) @@ -88,6 +99,15 @@ expect_no_corrections end + + it 'does not autocorrect `:+` without brackets when initial value is not provided' do + expect_offense(<<~RUBY, method: method) + array.#{method} :+ + ^{method}^^^ Use `sum` instead of `#{method}(:+)`, unless calling `#{method}(:+)` on an empty array. + RUBY + + expect_no_corrections + end end context 'when `SafeAutoCorrect: false' do @@ -114,6 +134,17 @@ array.sum RUBY end + + it 'autocorrects `:+` without brackets when initial value is not provided' do + expect_offense(<<~RUBY, method: method) + array.#{method} :+ + ^{method}^^^ Use `sum` instead of `#{method}(:+)`, unless calling `#{method}(:+)` on an empty array. + RUBY + + expect_correction(<<~RUBY) + array.sum + RUBY + end end it "registers an offense and corrects when using `array.#{method}(0, &:+)`" do