Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance/BlockGivenWithExplicitBlock throws an error with Ruby 3.1 anonymous blocks #281

Closed
nickcampbell18 opened this issue Jan 11, 2022 · 0 comments · Fixed by #282
Closed
Labels
bug Something isn't working

Comments

@nickcampbell18
Copy link

Performance/BlockGivenWithExplicitBlock does not seem to be able to handle anonymous blocks well in Ruby 3.1. I had this failure on one of my applications with the following code:

def method(foo, &)
  raise "block needed" unless block_given?
  do_something(&)
end

Expected behaviour

This behaviour may be undefined? I would probably expect this cop to either:

  1. Tell me that I should give the block a name (e.g. &block and then change my code to raise "..." unless block)
  2. Report no offenses

Actual behaviour

Currently, this code crashes Rubocop:

Failures:

  1) RuboCop::Cop::Performance::BlockGivenWithExplicitBlock in Ruby 3.1 does not break when given an anonymous block
     Failure/Error: block_arg_name = block_arg.loc.name.source.to_sym

     NoMethodError:
       undefined method `source' for nil:NilClass

                 block_arg_name = block_arg.loc.name.source.to_sym
                                                    ^^^^^^^
     # ./lib/rubocop/cop/performance/block_given_with_explicit_block.rb:42:in `on_send'

I've written a test case below to help identify & fix the problem if that helps:

diff --git a/spec/rubocop/cop/performance/block_given_with_explicit_block_spec.rb b/spec/rubocop/cop/performance/block_given_with_explicit_block_spec.rb
index 92e944ce9..afbba760e 100644
--- a/spec/rubocop/cop/performance/block_given_with_explicit_block_spec.rb
+++ b/spec/rubocop/cop/performance/block_given_with_explicit_block_spec.rb
@@ -68,4 +68,17 @@ RSpec.describe RuboCop::Cop::Performance::BlockGivenWithExplicitBlock, :config d
       do_something if block_given?
     RUBY
   end
+
+  describe 'in Ruby 3.1' do
+    let(:ruby_version) { 3.1 }
+
+    it 'does not break when given an anonymous block' do
+      expect_no_offenses(<<~RUBY)
+        def method(x, &)
+          raise ArgumentError, "block required" unless block_given?
+          do_something(&)
+        end
+      RUBY
+    end
+  end
 end

Rubocop version

I can reproduce this on the master branch:

  • Ruby 3.1.0
  • rubocop (1.24.1 c651461)
  • rubocop-ast (1.15.1)
  • rubocop-performance (1.13.1)
  • rubocop-rspec (2.5.0)
@koic koic added the bug Something isn't working label Jan 11, 2022
koic added a commit to koic/rubocop-performance that referenced this issue Jan 11, 2022
…tBlock`

Fixes rubocop#281.

This PR fixes an error for `Performance/BlockGivenWithExplicitBlock`
when using Ruby 3.1's anonymous block forwarding.
@koic koic closed this as completed in #282 Jan 14, 2022
koic added a commit that referenced this issue Jan 14, 2022
…given_with_explicit_block

[Fix #281] Fix an error for `Performance/BlockGivenWithExplicitBlock`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants