diff --git a/CHANGELOG.md b/CHANGELOG.md index f080bd7ea6f..c1e9a5e5194 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## master (unreleased) +### Bug fixes +* [#6855](https://github.com/rubocop-hq/rubocop/pull/6855): Fix an exception in `Rails/RedundantReceiverInWithOptions` when the body is empty. ([@ericsullivan][]) + ### Changes * [#5977](https://github.com/rubocop-hq/rubocop/issues/5977): Warn for Performance Cops. ([@koic][]) @@ -3882,3 +3885,4 @@ [@elmasantos]: https://github.com/elmasantos [@luciamo]: https://github.com/luciamo [@dirtyharrycallahan]: https://github.com/dirtyharrycallahan +[@ericsullivan]: https://github.com/ericsullivan diff --git a/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb b/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb index ef24d66a1f0..5b43ab6399b 100644 --- a/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +++ b/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb @@ -81,6 +81,7 @@ class RedundantReceiverInWithOptions < Cop def on_block(node) with_options?(node) do |arg, body| + return if body.nil? return unless all_block_nodes_in(body).count.zero? send_nodes = all_send_nodes_in(body) diff --git a/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb b/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb index 35859c69134..0e491e0bdd7 100644 --- a/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb +++ b/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb @@ -87,12 +87,20 @@ class Account < ApplicationRecord end RUBY end + + it 'does not register an offense when empty' do + expect_no_offenses(<<-RUBY.strip_indent) + with_options options: false do |merger| + end + RUBY + end end context 'rails <= 4.1' do let(:rails_version) { 4.1 } - it 'registers an offense when using explicit receiver in `with_options`' do + it 'does not register an offense when using explicit receiver in ' \ + '`with_options`' do expect_no_offenses(<<-RUBY.strip_indent) class Account < ApplicationRecord with_options dependent: :destroy do |assoc|