Class: RuboCop::Cop::Performance::Count
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Performance::Count
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/performance/count.rb
Overview
Identifies usages of ‘count` on an `Enumerable` that follow calls to `select`, `find_all`, `filter` or `reject`. Querying logic can instead be passed to the `count` call.
Constant Summary collapse
- MSG =
'Use `count` instead of `%<selector>s...%<counter>s`.'
- RESTRICT_ON_SEND =
%i[count length size].freeze
Instance Method Summary collapse
- #on_send(node) ⇒ Object (also: #on_csend)
Instance Method Details
#on_send(node) ⇒ Object Also known as: on_csend
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rubocop/cop/performance/count.rb', line 62 def on_send(node) count_candidate?(node) do |selector_node, selector, counter| return unless eligible_node?(node) range = source_starting_at(node) do selector_node.loc.selector.begin_pos end add_offense(range, message: format(MSG, selector: selector, counter: counter)) do |corrector| autocorrect(corrector, node, selector_node, selector) end end end |