Class: RuboCop::Cop::Lint::UnexpectedBlockArity

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

Overview

Checks for a block that is known to need more positional block arguments than are given (by default this is configured for Enumerable methods needing 2 arguments). Optional arguments are allowed, although they don’t generally make sense as the default value will be used. Blocks that have no receiver, or take splatted arguments (ie. *args) are always accepted.

Keyword arguments (including **kwargs) do not get counted towards this, as they are not used by the methods in question.

Method names and their expected arity can be configured like this:

Methods:
  inject: 2
  reduce: 2

Examples:

# bad
values.reduce {}
values.min { |a| a }
values.sort { |a; b| a + b }

# good
values.reduce { |memo, obj| memo << obj }
values.min { |a, b| a <=> b }
values.sort { |*x| x[0] <=> x[1] }

Cop Safety Information:

  • This cop matches for method names only and hence cannot tell apart methods with same name in different classes, which may lead to a false positive.

Constant Summary collapse

MSG =
'`%<method>s` expects at least %<expected>i positional arguments, got %<actual>i.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

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, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #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, #ready, #relevant_file?, 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_block(node) ⇒ Object Also known as: on_numblock



44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/lint/unexpected_block_arity.rb', line 44

def on_block(node)
  return if acceptable?(node)

  expected = expected_arity(node.method_name)
  actual = arg_count(node)
  return if actual >= expected

  message = format(MSG, method: node.method_name, expected: expected, actual: actual)
  add_offense(node, message: message)
end