Skip to content

Commit

Permalink
[Fix rubocop#8671] Fix an error for Style/ExplicitBlockArgument
Browse files Browse the repository at this point in the history
Fixes rubocop#8671.

This PR fixes an error for `Style/ExplicitBlockArgument` when using
safe navigation method call.
  • Loading branch information
koic committed Sep 9, 2020
1 parent c244e78 commit b75882d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/explicit_block_argument.rb
Expand Up @@ -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)')
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/explicit_block_argument_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit b75882d

Please sign in to comment.