Skip to content

Commit

Permalink
[Fix #9770] Update Lint/EmptyBlock to handle procs the same way as …
Browse files Browse the repository at this point in the history
…lambdas.
  • Loading branch information
dvandersluis authored and bbatsov committed May 7, 2021
1 parent 5ff1050 commit d1bd888
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/change_update_lintemptyblock_to_handle_procs.md
@@ -0,0 +1 @@
* [#9770](https://github.com/rubocop/rubocop/issues/9770): Update `Lint/EmptyBlock` to handle procs the same way as lambdas. ([@dvandersluis][])
20 changes: 18 additions & 2 deletions lib/rubocop/cop/lint/empty_block.rb
Expand Up @@ -7,7 +7,11 @@ module Lint
# Such empty blocks are typically an oversight or we should provide a comment
# be clearer what we're aiming for.
#
# Empty lambdas are ignored by default.
# Empty lambdas and procs are ignored by default.
#
# NOTE: For backwards compatibility, the configuration that allows/disallows
# empty lambdas and procs is called `AllowEmptyLambdas`, even though it also
# applies to procs.
#
# @example
# # bad
Expand Down Expand Up @@ -40,6 +44,10 @@ module Lint
# end
# (callable || placeholder).call
#
# proc { }
#
# Proc.new { }
#
# @example AllowEmptyLambdas: false
# # bad
# allow(subject).to receive(:callable).and_return(-> {})
Expand All @@ -48,12 +56,16 @@ module Lint
# end
# (callable || placeholder).call
#
# proc { }
#
# Proc.new { }
#
class EmptyBlock < Base
MSG = 'Empty block detected.'

def on_block(node)
return if node.body
return if allow_empty_lambdas? && node.lambda?
return if allow_empty_lambdas? && lambda_or_proc?(node)
return if cop_config['AllowComments'] && allow_comment?(node)

add_offense(node)
Expand All @@ -76,6 +88,10 @@ def comment_disables_cop?(comment)
regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})"
Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment)
end

def lambda_or_proc?(node)
node.lambda? || node.proc?
end
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/rubocop/cop/lint/empty_block_spec.rb
Expand Up @@ -43,6 +43,32 @@
RUBY
end

it 'does not register an offense on an empty proc' do
expect_no_offenses(<<~RUBY)
proc do
end
RUBY
end

it 'does not register an offense on an empty Proc.new' do
expect_no_offenses(<<~RUBY)
Proc.new {}
RUBY
end

it 'does not register an offense on an empty ::Proc.new' do
expect_no_offenses(<<~RUBY)
::Proc.new {}
RUBY
end

it 'registers an offense for an empty block given to a non-Kernel `proc` method' do
expect_offense(<<~RUBY)
Foo.proc {}
^^^^^^^^^^^ Empty block detected.
RUBY
end

context 'when AllowComments is false' do
let(:cop_config) { { 'AllowComments' => false } }

Expand Down Expand Up @@ -80,5 +106,27 @@
^^^^^ Empty block detected.
RUBY
end

it 'registers an offense on an empty proc' do
expect_offense(<<~RUBY)
proc do
^^^^^^^ Empty block detected.
end
RUBY
end

it 'registers an offense on an empty Proc.new' do
expect_offense(<<~RUBY)
Proc.new {}
^^^^^^^^^^^ Empty block detected.
RUBY
end

it 'registers an offense on an empty ::Proc.new' do
expect_offense(<<~RUBY)
::Proc.new {}
^^^^^^^^^^^^^ Empty block detected.
RUBY
end
end
end

0 comments on commit d1bd888

Please sign in to comment.