From 3da3d81eb2a868c5a04cb5954da40269e3277107 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 28 Feb 2024 16:50:13 +0900 Subject: [PATCH] Fix a false positive for `Style/ArgumentsForwarding` 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: https://github.com/rubocop/rubocop/issues/12571#issuecomment-1882650330 --- ...lse_positive_for_style_arguments_forwarding.md | 1 + lib/rubocop/cop/style/arguments_forwarding.rb | 11 ++++++++--- .../cop/style/arguments_forwarding_spec.rb | 15 +++------------ 3 files changed, 12 insertions(+), 15 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_style_arguments_forwarding.md diff --git a/changelog/fix_a_false_positive_for_style_arguments_forwarding.md b/changelog/fix_a_false_positive_for_style_arguments_forwarding.md new file mode 100644 index 000000000000..e76a97b73c65 --- /dev/null +++ b/changelog/fix_a_false_positive_for_style_arguments_forwarding.md @@ -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][]) diff --git a/lib/rubocop/cop/style/arguments_forwarding.rb b/lib/rubocop/cop/style/arguments_forwarding.rb index 652461054f38..d3813d31d95e 100644 --- a/lib/rubocop/cop/style/arguments_forwarding.rb +++ b/lib/rubocop/cop/style/arguments_forwarding.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/style/arguments_forwarding_spec.rb b/spec/rubocop/cop/style/arguments_forwarding_spec.rb index ceffccdc1e4f..aa709a986494 100644 --- a/spec/rubocop/cop/style/arguments_forwarding_spec.rb +++ b/spec/rubocop/cop/style/arguments_forwarding_spec.rb @@ -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