Skip to content

Commit

Permalink
Use Node#any_block_type?
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Jan 27, 2025
1 parent 79be0f2 commit 61bd21a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/cop/performance/chain_array_allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ChainArrayAllocation < Base
def_node_matcher :chain_array_allocation?, <<~PATTERN
(send {
(send _ $%RETURN_NEW_ARRAY_WHEN_ARGS {int lvar ivar cvar gvar send})
({block numblock} (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
(any_block (send _ $%ALWAYS_RETURNS_NEW_ARRAY) ...)
(send _ $%RETURNS_NEW_ARRAY ...)
} $%HAS_MUTATION_ALTERNATIVE ...)
PATTERN
Expand All @@ -75,7 +75,7 @@ def on_send(node)
def enumerable_select_method?(node)
# NOTE: `QueryMethods#select` in Rails accepts positional arguments, whereas `Enumerable#select` does not.
# This difference can be utilized to reduce the knowledge requirements related to `select`.
(node.block_type? || node.numblock_type?) && node.send_node.arguments.empty?
node.any_block_type? && node.send_node.arguments.empty?
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/cop/performance/times_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class TimesMap < Base
MESSAGE_ONLY_IF = 'only if `%<count>s` is always 0 or more'
RESTRICT_ON_SEND = %i[map collect].freeze

def_node_matcher :times_map_call, <<~PATTERN
{
(any_block $(call (call $!nil? :times) {:map :collect}) ...)
$(call (call $!nil? :times) {:map :collect} (block_pass ...))
}
PATTERN

def on_send(node)
check(node)
end
Expand Down Expand Up @@ -75,13 +82,6 @@ def message(map_or_collect, count)
end
format(template, count: count.source, map_or_collect: map_or_collect.method_name)
end

def_node_matcher :times_map_call, <<~PATTERN
{
({block numblock} $(call (call $!nil? :times) {:map :collect}) ...)
$(call (call $!nil? :times) {:map :collect} (block_pass ...))
}
PATTERN
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rubocop-performance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ Gem::Specification.new do |s|
}

s.add_dependency('rubocop', '>= 1.48.1', '< 2.0')
s.add_dependency('rubocop-ast', '>= 1.31.1', '< 2.0')
s.add_dependency('rubocop-ast', '>= 1.38.0', '< 2.0')
end

0 comments on commit 61bd21a

Please sign in to comment.