Class: RuboCop::Cop::Style::CollectionCompact

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

Overview

Checks for places where custom logic on rejection nils from arrays and hashes can be replaced with ‘Array,Hash#compact,compact!`.

Examples:

# bad
array.reject(&:nil?)
array.reject { |e| e.nil? }
array.select { |e| !e.nil? }
array.grep_v(nil)
array.grep_v(NilClass)

# good
array.compact

# bad
hash.reject!(&:nil?)
array.delete_if(&:nil?)
hash.reject! { |k, v| v.nil? }
array.delete_if { |e| e.nil? }
hash.select! { |k, v| !v.nil? }

# good
hash.compact!

AllowedReceivers: [‘params’]

# good
params.reject(&:nil?)

Constant Summary collapse

MSG =
'Use `%<good>s` instead of `%<bad>s`.'
RESTRICT_ON_SEND =
%i[reject delete_if reject! select select! grep_v].freeze
TO_ENUM_METHODS =
%i[to_enum lazy].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

minimum_target_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods included from AllowedReceivers

#allowed_receiver?, #allowed_receivers, #receiver_name

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, 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

#grep_v_with_nil?(node) ⇒ Object



85
86
87
# File 'lib/rubocop/cop/style/collection_compact.rb', line 85

def_node_matcher :grep_v_with_nil?, <<~PATTERN
  (send _ :grep_v {(nil) (const {nil? cbase} :NilClass)})
PATTERN

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubocop/cop/style/collection_compact.rb', line 89

def on_send(node)
  return unless (range = offense_range(node))
  return if allowed_receiver?(node.receiver)
  if (target_ruby_version <= 3.0 || node.method?(:delete_if)) && to_enum_method?(node)
    return
  end

  good = good_method_name(node)
  message = format(MSG, good: good, bad: range.source)

  add_offense(range, message: message) { |corrector| corrector.replace(range, good) }
end

#reject_method?(node) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/style/collection_compact.rb', line 64

def_node_matcher :reject_method?, <<~PATTERN
  (block
    (call
      !nil? {:reject :delete_if :reject!})
    $(args ...)
    (call
      $(lvar _) :nil?))
PATTERN

#reject_method_with_block_pass?(node) ⇒ Object



57
58
59
60
61
# File 'lib/rubocop/cop/style/collection_compact.rb', line 57

def_node_matcher :reject_method_with_block_pass?, <<~PATTERN
  (call !nil? {:reject :delete_if :reject!}
    (block_pass
      (sym :nil?)))
PATTERN

#select_method?(node) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rubocop/cop/style/collection_compact.rb', line 74

def_node_matcher :select_method?, <<~PATTERN
  (block
    (call
      !nil? {:select :select!})
    $(args ...)
    (call
      (call
        $(lvar _) :nil?) :!))
PATTERN