Skip to content

Commit

Permalink
Fix an error for Style/RedundantEach when using reverse_each.each
Browse files Browse the repository at this point in the history
… without a block
  • Loading branch information
Earlopain committed Mar 11, 2024
1 parent 1973bad commit 18f967d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_redundant_each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12780](https://github.com/rubocop/rubocop/issues/12780): Fix an error for `Style/RedundantEach` when using `reverse_each.each` without a block. ([@earlopain][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def redundant_each_method(node)
def range(node)
return node.selector unless node.method?(:each)

if node.parent.call_type?
if node.parent&.call_type?
node.selector.join(node.parent.loc.dot)
else
node.loc.dot.join(node.selector)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/redundant_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@
RUBY
end

it 'registers an offense when using `reverse_each.each` without a block' do
expect_offense(<<~RUBY)
context.reverse_each.each
^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
context.reverse_each
RUBY
end

it 'registers an offense when using `reverse_each&.each`' do
expect_offense(<<~RUBY)
context.reverse_each&.each { |i| do_something(i) }
Expand Down

0 comments on commit 18f967d

Please sign in to comment.