-
-
Notifications
You must be signed in to change notification settings - Fork 88
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
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(&)
endExpected behaviour
This behaviour may be undefined? I would probably expect this cop to either:
- Tell me that I should give the block a name (e.g.
&blockand then change my code toraise "..." unless block) - 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
endRubocop 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
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working