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 }

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, #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_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_literal?

  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