Skip to content

Commit 1d75e8b

Browse files
marcandremergify[bot]
authored andcommitted
Small refactor of #9127
1 parent a05c2e6 commit 1d75e8b

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

lib/rubocop/cop/lint/unexpected_block_arity.rb

+8-15
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class UnexpectedBlockArity < Base
3939
def on_block(node)
4040
return if acceptable?(node)
4141

42-
# If there is a splat argument, then the arity is fine
43-
return true if node.arguments.any?(&:restarg_type?)
44-
4542
expected = expected_arity(node.method_name)
4643
actual = arg_count(node)
4744
return if actual >= expected
@@ -50,17 +47,7 @@ def on_block(node)
5047
add_offense(node, message: message)
5148
end
5249

53-
def on_numblock(node)
54-
return if acceptable?(node)
55-
56-
expected = expected_arity(node.method_name)
57-
actual = node.children[1] # the maximum numbered param for the block
58-
59-
return if actual >= expected
60-
61-
message = format(MSG, method: node.method_name, expected: expected, actual: actual)
62-
add_offense(node, message: message)
63-
end
50+
alias on_numblock on_block
6451

6552
private
6653

@@ -81,10 +68,16 @@ def expected_arity(method)
8168
end
8269

8370
def arg_count(node)
71+
return node.children[1] if node.numblock_type? # the maximum numbered param for the block
72+
8473
# Only `arg`, `optarg` and `mlhs` (destructuring) count as arguments that
8574
# can be used. Keyword arguments are not used for these methods so are
8675
# ignored.
87-
node.arguments.count { |arg| arg.arg_type? || arg.optarg_type? || arg.mlhs_type? }
76+
node.arguments.count do |arg|
77+
return Float::INFINITY if arg.restarg_type?
78+
79+
arg.arg_type? || arg.optarg_type? || arg.mlhs_type?
80+
end
8881
end
8982
end
9083
end

0 commit comments

Comments
 (0)