diff --git a/changelog/change_update_lintemptyblock_to_handle_procs.md b/changelog/change_update_lintemptyblock_to_handle_procs.md new file mode 100644 index 00000000000..a695d53bbc0 --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/lint/empty_block.rb b/lib/rubocop/cop/lint/empty_block.rb index 6e7eaeb946d..006fcc16614 100644 --- a/lib/rubocop/cop/lint/empty_block.rb +++ b/lib/rubocop/cop/lint/empty_block.rb @@ -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 @@ -40,6 +44,10 @@ module Lint # end # (callable || placeholder).call # + # proc { } + # + # Proc.new { } + # # @example AllowEmptyLambdas: false # # bad # allow(subject).to receive(:callable).and_return(-> {}) @@ -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) @@ -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 diff --git a/spec/rubocop/cop/lint/empty_block_spec.rb b/spec/rubocop/cop/lint/empty_block_spec.rb index e5f3557f081..d13b4793c6d 100644 --- a/spec/rubocop/cop/lint/empty_block_spec.rb +++ b/spec/rubocop/cop/lint/empty_block_spec.rb @@ -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 } } @@ -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