Skip to content

Commit c21a6e6

Browse files
committed
Fix Performance/Count cop error on empty selector block
Something like ```ruby collection.reject { }.size ``` would lead to an error
1 parent d080ec3 commit c21a6e6

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#497](https://github.com/rubocop/rubocop-performance/pull/497): Fix `Performance/Count` cop error on empty selector block. ([@viralpraxis][])

lib/rubocop/cop/performance/count.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ def autocorrect(corrector, node, selector_node, selector)
8787
end
8888

8989
def eligible_node?(node)
90-
!(node.parent && node.parent.block_type?)
90+
return false if node.parent&.block_type?
91+
92+
node.receiver.call_type? || node.receiver.body
9193
end
9294

9395
def source_starting_at(node)

spec/rubocop/cop/performance/count_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,12 @@ def count(&block)
353353
RUBY
354354
end
355355
end
356+
357+
context 'with `reject` with empty block body' do
358+
it 'does not register an offense' do
359+
expect_no_offenses(<<~RUBY)
360+
array.reject {}.size
361+
RUBY
362+
end
363+
end
356364
end

0 commit comments

Comments
 (0)