class RuboCop::Cop::Style::RedundantFilterChain
arr.many? { |x| x > 1 }
# good
arr.select { |x| x > 1 }.many?
# bad
@example AllCops:ActiveSupportExtensionsEnabled: true
arr.select { |x| x > 1 }.many?
# good
@example AllCops:ActiveSupportExtensionsEnabled: false (default)
arr.select { |x| x > 1 }.any?(&:odd?)
relation.select(:name).any?
# good
arr.none? { |x| x > 1 }
# good
arr.select { |x| x > 1 }.none?
arr.select { |x| x > 1 }.empty?
# bad
arr.any? { |x| x > 1 }
# good
arr.select { |x| x > 1 }.any?
# bad
@example
chained to ‘select`/`filter`/`find_all` and change them to use predicate method instead.
Identifies usages of `any?`, `empty?` or `none?` predicate methods
def offense_range(select_node, predicate_node)
def offense_range(select_node, predicate_node) select_node.loc.selector.join(predicate_node.loc.selector) end
def on_send(node)
def on_send(node) return if node.arguments? || node.block_node select_predicate?(node) do |select_node, filter_method| return if RAILS_METHODS.include?(filter_method) && !active_support_extensions_enabled? register_offense(select_node, node) end end
def predicate_range(predicate_node)
def predicate_range(predicate_node) predicate_node.receiver.source_range.end.join(predicate_node.loc.selector) end
def register_offense(select_node, predicate_node)
def register_offense(select_node, predicate_node) replacement = REPLACEMENT_METHODS[predicate_node.method_name] message = format(MSG, prefer: replacement, first_method: select_node.method_name, second_method: predicate_node.method_name) offense_range = offense_range(select_node, predicate_node) add_offense(offense_range, message: message) do |corrector| corrector.remove(predicate_range(predicate_node)) corrector.replace(select_node.loc.selector, replacement) end end