class RuboCop::Cop::Performance::CollectionLiteralInLoop
end
ADMIN_ROLES.include?(user.role)
users.select do |user|
…
ADMIN_ROLES = %i[superadmin admin]
# good
end
admin_roles.include?(user.role)
users.select do |user|
admin_roles = %i[superadmin admin]
# good
end
%i[superadmin admin].include?(user.role)
users.select do |user|
# bad
@example
the array allocation.
‘[1, 2, 3].include?(@foo)` and `[1, 2, 3].include?(@foo.bar.baz)` both avoid
calls without arguments. Additionally, any number of methods can be chained:
includes: strings, `self`, local variables, instance variables, and method
code, as such no offense will be registered in those cases. Currently that
optimized directly in Ruby. This avoids allocations without changing the
NOTE: Since Ruby 3.4, certain simple arguments to `Array#include?` are
an offense with `MinSize`.
You can set the minimum number of elements to consider
to avoid unnecessary allocations on each iteration.
It is better to extract them into a local variable or constant
Identifies places where Array and Hash literals are used within loops.
def check_literal?(node, method, arguments)
def check_literal?(node, method, arguments) !node.nil? && nonmutable_method_of_array_or_hash?(node, method) && node.children.size >= min_size && node.recursive_basic_literal? && !optimized_array_include?(node, method, arguments) end
def enumerable_method?(method_name)
def enumerable_method?(method_name) ENUMERABLE_METHOD_NAMES.include?(method_name) end
def keyword_loop?(type)
def keyword_loop?(type) LOOP_TYPES.include?(type) end
def literal_class(node)
def literal_class(node) if node.array_type? 'Array' elsif node.hash_type? 'Hash' end end
def loop?(ancestor, node)
def loop?(ancestor, node) keyword_loop?(ancestor.type) || kernel_loop?(ancestor) || node_within_enumerable_loop?(node, ancestor) end
def min_size
def min_size Integer(cop_config['MinSize'] || 1) end
def node_within_enumerable_loop?(node, ancestor)
def node_within_enumerable_loop?(node, ancestor) enumerable_loop?(ancestor) do |receiver| receiver != node && !receiver&.descendants&.include?(node) end end
def nonmutable_method_of_array_or_hash?(node, method)
def nonmutable_method_of_array_or_hash?(node, method) (node.array_type? && ARRAY_METHODS.include?(method)) || (node.hash_type? && HASH_METHODS.include?(method)) end
def on_send(node)
def on_send(node) receiver, method, *arguments = *node.children return unless check_literal?(receiver, method, arguments) && parent_is_loop?(receiver) message = format(MSG, literal_class: literal_class(receiver)) add_offense(receiver, message: message) end
def optimized_array_include?(node, method, arguments)
See https://github.com/ruby/ruby/pull/12123 for more details.
Since Ruby 3.4, simple arguments to Array#include? are optimized.
def optimized_array_include?(node, method, arguments) return false unless target_ruby_version >= 3.4 && node.array_type? && method == :include? # Disallow include?(1, 2) return false if arguments.count != 1 arg = arguments.first # Allow `include?(foo.bar.baz.bat)` while arg.send_type? return false if arg.arguments.any? # Disallow include?(foo(bar)) break unless arg.receiver arg = arg.receiver end ARRAY_INCLUDE_OPTIMIZED_TYPES.include?(arg.type) end
def parent_is_loop?(node)
def parent_is_loop?(node) node.each_ancestor.any? { |ancestor| loop?(ancestor, node) } end