Class: RuboCop::Cop::Lint::ArgumentMismatch

Inherits:
Base
  • Object
show all
Includes:
IndexedMethodArity, ProjectIndexHelp
Defined in:
lib/rubocop/cop/lint/argument_mismatch.rb

Overview

Checks for calls that pass the wrong number of positional arguments to a method whose definition is known from the project index.

The check is powered by the project-wide index, so it only runs when AllCops/UseProjectIndex is enabled and the rubydex gem is installed. Without the index the cop does nothing.

Only calls on constant receivers (Foo.bar) are considered, and only when the receiver resolves in the index, its entire ancestry is resolved (so a method inherited from a gem or the standard library never produces an offense), and the called method resolves to a single, unambiguous signature. Calls that forward arguments (*, **, ...) are skipped because their argument count is not statically known. Keyword arguments passed to a method that declares no keyword parameters are counted as the single positional hash they collapse into, matching Ruby's semantics.

Constructor calls (Foo.new) and keyword-argument validation are out of scope: only positional arity of explicitly defined singleton methods is checked.

Examples:

# Given the project defines:
#   class Report
#     def self.generate(source, format); end
#   end

# bad
Report.generate(source)

# bad
Report.generate(source, :pdf, :extra)

# good
Report.generate(source, :pdf)

Constant Summary collapse

MSG =
'Wrong number of arguments to `%<method>s` (given %<given>d, expected %<expected>s).'

Constants included from ProjectIndexHelp

ProjectIndexHelp::BUILTIN_DOCUMENT_URI, ProjectIndexHelp::FILE_URI_PREFIX, ProjectIndexHelp::WINDOWS_DRIVE_PREFIX

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Instance Method Summary collapse

Methods included from ProjectIndexHelp

#external_dependency_checksum

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?, #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?, #skipped_unsafe_correction_with_disable_uncorrectable?

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



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

def on_send(node)
  return unless project_index
  return unless node.receiver&.const_type?

  shape = resolved_signature_shape(node)
  return unless shape

  min, max, has_keywords = shape
  given = positional_argument_count(node, has_keywords)
  return if given.nil? || arity_satisfied?(given, min, max)

  add_offense(node.loc.selector, message: message(node, given, min, max))
end