Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Style/ArgumentsForwarding #12720

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#12720](https://github.com/rubocop/rubocop/issues/12720): Fix a false positive for `Style/ArgumentsForwarding` when using block arg forwarding to within block with Ruby 3.3.0. ([@koic][])
11 changes: 8 additions & 3 deletions lib/rubocop/cop/style/arguments_forwarding.rb
Expand Up @@ -188,9 +188,12 @@ def add_forward_all_offenses(node, send_classifications, forwardable_args)

send_classifications.each do |send_node, _c, forward_rest, forward_kwrest, forward_block_arg| # rubocop:disable Layout/LineLength
if !forward_rest && !forward_kwrest
register_forward_block_arg_offense(!forward_rest, node.arguments, block_arg)
register_forward_block_arg_offense(!forward_rest, send_node, forward_block_arg)

# Prevents `anonymous block parameter is also used within block (SyntaxError)` occurs
# in Ruby 3.3.0.
if outside_block?(forward_block_arg)
register_forward_block_arg_offense(!forward_rest, node.arguments, block_arg)
register_forward_block_arg_offense(!forward_rest, send_node, forward_block_arg)
end
registered_block_arg_offense = true
break
else
Expand Down Expand Up @@ -222,6 +225,8 @@ def add_post_ruby_32_offenses(def_node, send_classifications, forwardable_args)
register_forward_kwargs_offense(!forward_rest, send_node, forward_kwrest)
end

# Prevents `anonymous block parameter is also used within block (SyntaxError)` occurs
# in Ruby 3.3.0.
if outside_block?(forward_block_arg)
register_forward_block_arg_offense(!forward_rest, def_node.arguments, block_arg)
register_forward_block_arg_offense(!forward_rest, send_node, forward_block_arg)
Expand Down
15 changes: 3 additions & 12 deletions spec/rubocop/cop/style/arguments_forwarding_spec.rb
Expand Up @@ -836,21 +836,12 @@ def baz(qux, quuz, &)
RUBY
end

it 'registers an offense when using block arg forwarding with positional arguments forwarding to within block' do
expect_offense(<<~RUBY)
# `anonymous block parameter is also used within block (SyntaxError)` occurs in Ruby 3.3.0:
it 'does not register an offense when using block arg forwarding with positional arguments forwarding to within block' do
expect_no_offenses(<<~RUBY)
def baz(qux, quuz, &block)
^^^^^^ Use anonymous block arguments forwarding (`&`).
with_block do
bar(qux, quuz, &block)
^^^^^^ Use anonymous block arguments forwarding (`&`).
end
end
RUBY

expect_correction(<<~RUBY)
def baz(qux, quuz, &)
with_block do
bar(qux, quuz, &)
end
end
RUBY
Expand Down