diff --git a/CHANGELOG.md b/CHANGELOG.md index 907816d76e4..f95394f725c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * [#8660](https://github.com/rubocop-hq/rubocop/pull/8660): Fix a false positive for `Style/ClassAndModuleChildren` when using cbase module name. ([@koic][]) * [#8664](https://github.com/rubocop-hq/rubocop/issues/8664): Fix a false positive for `Naming/BinaryOperatorParameterName` when naming multibyte character method name. ([@koic][]) * [#8604](https://github.com/rubocop-hq/rubocop/issues/8604): Fix a false positive for `Bundler/DuplicatedGem` when gem is duplciated in condition. ([@tejasbubane][]) +* [#8671](https://github.com/rubocop-hq/rubocop/issues/8671): Fix an error for `Style/ExplicitBlockArgument` when using safe navigation method call. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/explicit_block_argument.rb b/lib/rubocop/cop/style/explicit_block_argument.rb index e582cfc65fd..0300a675d18 100644 --- a/lib/rubocop/cop/style/explicit_block_argument.rb +++ b/lib/rubocop/cop/style/explicit_block_argument.rb @@ -83,7 +83,7 @@ def add_block_argument(node, corrector) replacement = ' &block' replacement = ",#{replacement}" unless arg_range.source.end_with?(',') corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type? - elsif node.send_type? + elsif node.call_type? corrector.insert_after(node, '(&block)') else corrector.insert_after(node.loc.name, '(&block)') diff --git a/spec/rubocop/cop/style/explicit_block_argument_spec.rb b/spec/rubocop/cop/style/explicit_block_argument_spec.rb index dac8f7f5d3a..be6d3f1357e 100644 --- a/spec/rubocop/cop/style/explicit_block_argument_spec.rb +++ b/spec/rubocop/cop/style/explicit_block_argument_spec.rb @@ -63,6 +63,23 @@ def m(&block) RUBY end + it 'correctly corrects when using safe navigation method call' do + expect_offense(<<~RUBY) + def do_something + array&.each do |row| + ^^^^^^^^^^^^^^^^^^^^ Consider using explicit block argument in the surrounding method's signature over `yield`. + yield row + end + end + RUBY + + expect_correction(<<~RUBY) + def do_something(&block) + array&.each(&block) + end + RUBY + end + it 'registers an offense and corrects when method contains multiple `yield`s' do expect_offense(<<~RUBY) def m