Skip to content

Commit

Permalink
[Fix #12687] Fix a false positive for Lint/Void
Browse files Browse the repository at this point in the history
Fixes #12687.

This PR fixes a false positive for `Lint/Void` when
`each` block with conditional expressions that has multiple statements.
  • Loading branch information
koic authored and bbatsov committed Feb 15, 2024
1 parent d8d8474 commit d4afee6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_false_positive_for_lint_void.md
@@ -0,0 +1 @@
* [#12687](https://github.com/rubocop/rubocop/issues/12687): Fix a false positive for `Lint/Void` when `each` block with conditional expressions that has multiple statements. ([@koic][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/lint/void.rb
Expand Up @@ -100,7 +100,12 @@ def check_begin(node)
expressions = *node
expressions.pop unless in_void_context?(node)
expressions.each do |expr|
check_void_op(expr)
check_void_op(expr) do
block_node = node.each_ancestor(:block).first

block_node&.method?(:each)
end

check_expression(expr)
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/void_spec.rb
Expand Up @@ -614,6 +614,18 @@ def foo=(rhs)
RUBY
end

it 'does not register `#each` block with conditional expressions that has multiple statements' do
expect_no_offenses(<<~RUBY)
enumerator_as_filter.each do |item|
puts item
# The `filter` method is used to filter for matches with `42`.
# In this case, it's not void.
item == 42
end
RUBY
end

context 'Ruby 2.7', :ruby27 do
it 'registers two offenses for void literals in `#tap` method' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit d4afee6

Please sign in to comment.