Skip to content

Commit

Permalink
Merge pull request #12770 from Earlopain/fix-an-error-for-lint-redund…
Browse files Browse the repository at this point in the history
…ant-with-index

Fix an error for `Lint/RedundantWithIndex` when there is no receiver
  • Loading branch information
koic committed Mar 10, 2024
2 parents 1281bf0 + 42884ee commit 5ee786d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_lint_redundant_with_index.md
@@ -0,0 +1 @@
* [#12770](https://github.com/rubocop/rubocop/pull/12770): Fix an error for `Lint/RedundantWithIndex` when the method has no receiver. ([@earlopain][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/lint/redundant_with_index.rb
Expand Up @@ -34,6 +34,7 @@ class RedundantWithIndex < Base
MSG_WITH_INDEX = 'Remove redundant `with_index`.'

def on_block(node)
return unless node.receiver
return unless (send = redundant_with_index?(node))

range = with_index_range(send)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/lint/redundant_with_index_spec.rb
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 5ee786d

Please sign in to comment.