Skip to content

Commit

Permalink
Fix an error for Lint/RedundantWithIndex when there is no receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Mar 10, 2024
1 parent 55cbc7f commit 5d0e3b1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_redundant_with_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12770](https://github.com/rubocop/rubocop/pull/12770): Fix an error for `Lint/RedundantWithIndex` when the method has no receiver. ([@earlopain][])
4 changes: 2 additions & 2 deletions lib/rubocop/cop/lint/redundant_with_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def on_block(node)
def_node_matcher :redundant_with_index?, <<~PATTERN
{
(block
$(call _ {:each_with_index :with_index} ...)
$(call ^_ {:each_with_index :with_index} ...)
(args (arg _)) ...)
(numblock
$(call _ {:each_with_index :with_index} ...) 1 ...)
$(call ^_ {:each_with_index :with_index} ...) 1 ...)
}
PATTERN

Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/lint/redundant_with_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,13 @@
it 'accepts an index is used as a numblock argument' do
expect_no_offenses('ary.each_with_index { _1; _2 }')
end

it 'accepts with_index without receiver with a block' do
expect_no_offenses('with_index { |v| v }')
end

it 'accepts with_index without receiver with a numblock' do
expect_no_offenses('with_index { _1 }')
end
end
end

0 comments on commit 5d0e3b1

Please sign in to comment.