Skip to content

Commit 97af229

Browse files
authored
Merge pull request #498 from viralpraxis/fix-performance-count-cop-error-on-empty-selector-block
Fix `Performance/Count` cop error on empty selector block
2 parents d080ec3 + 7b5aa7b commit 97af229

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#498](https://github.com/rubocop/rubocop-performance/pull/498): 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)