Class: RuboCop::Cop::Lint::ToEnumArguments

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/to_enum_arguments.rb

Overview

Ensures that ‘to_enum`/`enum_for`, called for the current method, has correct arguments.

Examples:

# bad
def foo(x, y = 1)
  return to_enum(__callee__, x) # `y` is missing
end

# good
def foo(x, y = 1)
  # Alternatives to `__callee__` are `__method__` and `:foo`.
  return to_enum(__callee__, x, y)
end

# good
def foo(x, y = 1)
  # It is also allowed if it is wrapped in some method like Sorbet.
  return to_enum(T.must(__callee__), x, y)
end

Constant Summary collapse

MSG =
'Ensure you correctly provided all the arguments.'
RESTRICT_ON_SEND =
%i[to_enum enum_for].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

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

#enum_conversion_call?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 33

def_node_matcher :enum_conversion_call?, <<~PATTERN
  (send {nil? self} {:to_enum :enum_for} $_ $...)
PATTERN

#method_name?(node, name) ⇒ Object



38
39
40
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 38

def_node_matcher :method_name?, <<~PATTERN
  {(send nil? {:__method__ :__callee__}) (sym %1)}
PATTERN

#on_send(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 47

def on_send(node)
  def_node = node.each_ancestor(:def, :defs).first
  return unless def_node

  enum_conversion_call?(node) do |method_node, arguments|
    next if !method_name?(method_node, def_node.method_name) ||
            arguments_match?(arguments, def_node)

    add_offense(node)
  end
end

#passing_keyword_arg?(node, name) ⇒ Object



43
44
45
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 43

def_node_matcher :passing_keyword_arg?, <<~PATTERN
  (pair (sym %1) (lvar %1))
PATTERN