Class: RuboCop::Cop::Style::RedundantFilterChain

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/redundant_filter_chain.rb

Overview

Identifies usages of any?, empty? or none? predicate methods chained to select/filter/find_all and change them to use predicate method instead.

Examples:

# bad
arr.select { |x| x > 1 }.any?

# good
arr.any? { |x| x > 1 }

# bad
arr.select { |x| x > 1 }.empty?
arr.select { |x| x > 1 }.none?

# good
arr.none? { |x| x > 1 }

# good
relation.select(:name).any?
arr.select { |x| x > 1 }.any?(&:odd?)

AllCops:ActiveSupportExtensionsEnabled: false (default)

# good
arr.select { |x| x > 1 }.many?

# good
arr.select { |x| x > 1 }.present?

AllCops:ActiveSupportExtensionsEnabled: true

# bad
arr.select { |x| x > 1 }.many?

# good
arr.many? { |x| x > 1 }

# bad
arr.select { |x| x > 1 }.present?

# good
arr.any? { |x| x > 1 }

Cop Safety Information:

  • This cop’s autocorrection is unsafe because array.select.any? evaluates all elements through the select method, while array.any? uses short-circuit evaluation. In other words, array.select.any? guarantees the evaluation of every element, but array.any? does not necessarily evaluate all of them.

Constant Summary collapse

MSG =
'Use `%<prefer>s` instead of `%<first_method>s.%<second_method>s`.'
RAILS_METHODS =
%i[many? present?].freeze
RESTRICT_ON_SEND =
(%i[any? empty? none? one?] + RAILS_METHODS).freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



81
82
83
84
85
86
87
88
89
# File 'lib/rubocop/cop/style/redundant_filter_chain.rb', line 81

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

#select_predicate?(node) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/rubocop/cop/style/redundant_filter_chain.rb', line 62

def_node_matcher :select_predicate?, <<~PATTERN
  (call
    {
      (block $(call _ {:select :filter :find_all}) ...)
      $(call _ {:select :filter :find_all} block_pass_type?)
    }
    ${:#{RESTRICT_ON_SEND.join(' :')}})
PATTERN