Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error in Performance/Sum when method has no brackets #264

Merged
merged 1 commit into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,4 @@
[@dvandersluis]: https://github.com/dvandersluis
[@ghiculescu]: https://github.com/ghiculescu
[@mfbmina]: https://github.com/mfbmina
[@mvz]: https://github.com/mvz
1 change: 1 addition & 0 deletions changelog/fix_fix_error_in_performancesum_when_method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#264](https://github.com/rubocop/rubocop-performance/pull/264): Fix error in Performance/Sum when method has no brackets. ([@mvz][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/performance/sum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, :+)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down