Skip to content

Commit

Permalink
[Fix rubocop#3266] Handle empty parentheses in RedundantBlockCall (ru…
Browse files Browse the repository at this point in the history
…bocop#3293)

The autocorrect method should not assume that parentheses means
there are arguments. If the block is called with empty
parentheses, we replace it with a `yield` without parentheses.
  • Loading branch information
jonas054 authored and Neodelf committed Oct 15, 2016
1 parent babec93 commit 46b5300
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [#3247](https://github.com/bbatsov/rubocop/issues/3247): Ensure whitespace after beginning of block in `Style/BlockDelimiters`. ([@tjwp][])
* [#2941](https://github.com/bbatsov/rubocop/issues/2941): Make sure `Lint/UnneededDisable` can do auto-correction. ([@jonas054][])
* [#3269](https://github.com/bbatsov/rubocop/pull/3269): Fix `Lint/ShadowedException` to block arbitrary code execution. ([@pocke][])
* [#3266](https://github.com/bbatsov/rubocop/issues/3266): Handle empty parentheses in `Performance/RedundantBlockCall` auto-correct. ([@jonas054][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/redundant_block_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def autocorrect(node)
new_source << args.map(&:source).join(', ')
end

new_source << CLOSE_PAREN if parentheses?(node)
new_source << CLOSE_PAREN if parentheses?(node) && !args.empty?
->(corrector) { corrector.replace(node.source_range, new_source) }
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/performance/redundant_block_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
'end'].join("\n"))
end

it 'autocorrects block.call with empty parentheses' do
new_source = autocorrect_source(cop, ['def method(&block)',
' block.call()',
'end'])
expect(new_source).to eq(['def method(&block)',
' yield',
'end'].join("\n"))
end

it 'autocorrects block.call with arguments' do
new_source = autocorrect_source(cop, ['def method(&block)',
' block.call 1, 2',
Expand Down

0 comments on commit 46b5300

Please sign in to comment.