Skip to content

Commit 933c879

Browse files
authored
Merge pull request #13783 from Earlopain/lint-void-numblock
Fix a false positive for `Lint/Void` when `each` numblock with conditional expressions that has multiple statements
2 parents 8a48e61 + ba9f23b commit 933c879

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13783](https://github.com/rubocop/rubocop/pull/13783): Fix a false positive for `Lint/Void` when `each` numblock with conditional expressions that has multiple statements. ([@earlopain][])

lib/rubocop/cop/lint/void.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def check_begin(node)
103103
expressions.pop unless in_void_context?(node)
104104
expressions.each do |expr|
105105
check_void_op(expr) do
106-
block_node = node.each_ancestor(:block).first
106+
block_node = node.each_ancestor(:any_block).first
107107

108108
block_node&.method?(:each)
109109
end
@@ -201,11 +201,6 @@ def check_ensure(node)
201201
# NOTE: the `begin` node case is already handled via `on_begin`
202202
return if body.begin_type?
203203

204-
check_void_op(body) do
205-
block_node = node.each_ancestor(:block).first
206-
block_node&.method?(:each)
207-
end
208-
209204
check_expression(body)
210205
end
211206

spec/rubocop/cop/lint/void_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,18 @@ def self.foo=(rhs)
894894
RUBY
895895
end
896896

897+
it 'does not register `#each` numblock with conditional expressions that has multiple statements' do
898+
expect_no_offenses(<<~RUBY)
899+
enumerator_as_filter.each do
900+
puts _1
901+
902+
# The `filter` method is used to filter for matches with `42`.
903+
# In this case, it's not void.
904+
_1 == 42
905+
end
906+
RUBY
907+
end
908+
897909
context 'Ruby 2.7', :ruby27 do
898910
it 'registers two offenses for void literals in `#tap` method' do
899911
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy