Skip to content

Commit

Permalink
Merge pull request #12566 from koic/make_style_redundant_each_aware_o…
Browse files Browse the repository at this point in the history
…f_safe_navigation_operator

[Fix #12453] Make `Style/RedundantEach` aware of safe navigation operator
  • Loading branch information
koic committed Dec 23, 2023
2 parents 14e26fd + 81ef82d commit 38bcf13
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12453](https://github.com/rubocop/rubocop/issues/12453): Make `Style/RedundantEach` aware of safe navigation operator. ([@koic][])
11 changes: 7 additions & 4 deletions lib/rubocop/cop/style/redundant_each.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def on_send(node)
end
end
end
alias on_csend on_send

private

Expand All @@ -64,7 +65,7 @@ def redundant_each_method(node)
return if node.last_argument&.block_pass_type?

if node.method?(:each) && !node.parent&.block_type?
ancestor_node = node.each_ancestor(:send).detect do |ancestor|
ancestor_node = node.each_ancestor(:send, :csend).detect do |ancestor|
ancestor.receiver == node &&
(RESTRICT_ON_SEND.include?(ancestor.method_name) || ancestor.method?(:reverse_each))
end
Expand All @@ -83,10 +84,12 @@ def redundant_each_method(node)
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

def range(node)
if node.method?(:each)
node.loc.dot.join(node.loc.selector)
return node.selector unless node.method?(:each)

if node.parent.call_type?
node.selector.join(node.parent.loc.dot)
else
node.loc.selector
node.loc.dot.join(node.selector)
end
end

Expand Down
54 changes: 49 additions & 5 deletions spec/rubocop/cop/style/redundant_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,51 @@
it 'registers an offense when using `each.each`' do
expect_offense(<<~RUBY)
array.each.each { |v| do_something(v) }
^^^^^ Remove redundant `each`.
^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
array.each { |v| do_something(v) }
RUBY
end

it 'registers an offense when using `each&.each`' do
expect_offense(<<~RUBY)
array.each&.each { |v| do_something(v) }
^^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
array.each { |v| do_something(v) }
RUBY
end

it 'registers an offense when using `&.each.each`' do
expect_offense(<<~RUBY)
array&.each.each { |v| do_something(v) }
^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
array&.each { |v| do_something(v) }
RUBY
end

it 'registers an offense when using `&.each&.each`' do
expect_offense(<<~RUBY)
array&.each&.each { |v| do_something(v) }
^^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
array&.each { |v| do_something(v) }
RUBY
end

it 'registers an offense when using `each.each(&:foo)`' do
expect_offense(<<~RUBY)
array.each.each(&:foo)
^^^^^ Remove redundant `each`.
^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -26,7 +59,7 @@
it 'registers an offense when using `each.each_with_index`' do
expect_offense(<<~RUBY)
array.each.each_with_index { |v| do_something(v) }
^^^^^ Remove redundant `each`.
^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -37,7 +70,7 @@
it 'registers an offense when using `each.each_with_object`' do
expect_offense(<<~RUBY)
array.each.each_with_object([]) { |v, o| do_something(v, o) }
^^^^^ Remove redundant `each`.
^^^^^ Remove redundant `each`.
RUBY

expect_correction(<<~RUBY)
Expand Down Expand Up @@ -82,10 +115,21 @@
RUBY
end

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

expect_correction(<<~RUBY)
context.reverse_each { |i| do_something(i) }
RUBY
end

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

expect_correction(<<~RUBY)
Expand Down

0 comments on commit 38bcf13

Please sign in to comment.