Skip to content

Commit

Permalink
Fix a false positive for Style/ArgumentsForwarding
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Style/ArgumentsForwarding`
when using block arg forwarding to within block with Ruby 3.3.0.

The following `anonymous block parameter is also used within block (SyntaxError)` occurs in Ruby 3.3.0:

```console
$ cat example.rb
def baz(qux, quuz, &)
  with_block do
    bar(qux, quuz, &)
  end
end

$ ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
example.rb: example.rb:3: anonymous block parameter is also used within block (SyntaxError)
```

This fix is based on the following comment:
#12571 (comment)
  • Loading branch information
koic authored and bbatsov committed Feb 28, 2024
1 parent 6758218 commit 8aae48b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8aae48b

Please sign in to comment.