class RuboCop::Cop::Lint::UnexpectedBlockArity
values.sort { |*x| x <=> x }
values.min { |a, b| a <=> b }
values.reduce { |memo, obj| memo << obj }
# good
values.sort { |a; b| a + b }
values.min { |a| a }
values.reduce {}
# bad
@example
false positive.
methods with same name in different classes, which may lead to a
This cop matches for method names only and hence cannot tell apart
@safety
—-
reduce: 2
inject: 2
Methods:
—-
[source,yaml]
Method names and their expected arity can be configured like this:
this, as they are not used by the methods in question.
Keyword arguments (including ‘**kwargs`) do not get counted towards
(ie. `*args`) are always accepted.
be used. Blocks that have no receiver, or take splatted arguments
although they don’t generally make sense as the default value will
`Enumerable` methods needing 2 arguments). Optional arguments are allowed,
block arguments than are given (by default this is configured for
This cop checks for a block that is known to need more positional
def acceptable?(node)
def acceptable?(node) !(included_method?(node.method_name) && node.receiver) end
def arg_count(node)
def arg_count(node) return node.children[1] if node.numblock_type? # the maximum numbered param for the block # Only `arg`, `optarg` and `mlhs` (destructuring) count as arguments that # can be used. Keyword arguments are not used for these methods so are # ignored. node.arguments.count do |arg| return Float::INFINITY if arg.restarg_type? arg.arg_type? || arg.optarg_type? || arg.mlhs_type? end end
def expected_arity(method)
def expected_arity(method) cop_config['Methods'][method.to_s] end
def included_method?(name)
def included_method?(name) methods.key?(name.to_s) end
def methods
def methods cop_config.fetch('Methods', []) end
def on_block(node)
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