Class: RuboCop::Cop::Style::PartitionInsteadOfDoubleSelect

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

Overview

Checks for consecutive calls to ‘select`/`filter`/`find_all` and `reject` on the same receiver with the same block body, where `partition` could be used instead. Also detects two `select` or two `reject` calls where one block negates the other with `!`. Using `partition` reduces two collection traversals to one.

Examples:

# bad
positives = array.select { |x| x > 0 }
negatives = array.reject { |x| x > 0 }

# bad
positives = array.filter { |x| x > 0 }
negatives = array.reject { |x| x > 0 }

# bad
negatives = array.reject { |x| x > 0 }
positives = array.select { |x| x > 0 }

# bad
positives = array.select(&:positive?)
negatives = array.reject(&:positive?)

# bad
positives = array.select(&:positive?)
negatives = array.reject { |x| x.positive? }

# bad
positives = array.select { |x| x.positive? }
non_positives = array.select { |x| !x.positive? }

# good
positives, negatives = array.partition { |x| x > 0 }

# good
positives, non_positives = array.partition { |x| x.positive? }

# good
positives, negatives = array.partition(&:positive?)

Constant Summary collapse

MSG =
'Use `partition` instead of consecutive `%<first>s` and `%<second>s` calls.'
SELECT_METHODS =
%i[select filter find_all].freeze
CANDIDATE_METHODS =
(SELECT_METHODS + %i[reject]).to_set.freeze
RESTRICT_ON_SEND =
(SELECT_METHODS + %i[reject]).freeze

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

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, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #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, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

cop_dir_for, #exclude_limit, read_limits

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_block(node) ⇒ Object Also known as: on_numblock, on_itblock



71
72
73
74
75
# File 'lib/rubocop/cop/style/partition_instead_of_double_select.rb', line 71

def on_block(node)
  return unless CANDIDATE_METHODS.include?(node.method_name)

  find_and_register_offense(node)
end

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



79
80
81
82
83
# File 'lib/rubocop/cop/style/partition_instead_of_double_select.rb', line 79

def on_send(node)
  return unless node.last_argument&.block_pass_type?

  find_and_register_offense(node)
end

#symbol_proc_method?(node) ⇒ Object



67
68
69
# File 'lib/rubocop/cop/style/partition_instead_of_double_select.rb', line 67

def_node_matcher :symbol_proc_method?, <<~PATTERN
  (block _ (args (arg _name)) (send (lvar _name) $_method_name))
PATTERN