diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a12b28b..bcd1baa22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) - Fix beginless and endless range bug for RepeatedIncludeExample cop. ([@hasghari]) +- Fix a false positive for `RSpec/RepeatedSubjectCall` when subject is used as argument to function call. ([@K-S-A]) ## 2.29.1 (2024-04-05) @@ -910,6 +911,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@jojos003]: https://github.com/jojos003 [@jonatas]: https://github.com/jonatas [@jtannas]: https://github.com/jtannas +[@k-s-a]: https://github.com/K-S-A [@kellysutton]: https://github.com/kellysutton [@koic]: https://github.com/koic [@kuahyeow]: https://github.com/kuahyeow diff --git a/lib/rubocop/cop/rspec/repeated_subject_call.rb b/lib/rubocop/cop/rspec/repeated_subject_call.rb index faa430718..15bcffc60 100644 --- a/lib/rubocop/cop/rspec/repeated_subject_call.rb +++ b/lib/rubocop/cop/rspec/repeated_subject_call.rb @@ -72,6 +72,7 @@ def on_top_level_group(node) def detect_offense(subject_node) return if subject_node.chained? + return if subject_node.parent.send_type? return unless (block_node = expect_block(subject_node)) add_offense(block_node) diff --git a/spec/rubocop/cop/rspec/repeated_subject_call_spec.rb b/spec/rubocop/cop/rspec/repeated_subject_call_spec.rb index 2c11c3e0f..0ea1f3151 100644 --- a/spec/rubocop/cop/rspec/repeated_subject_call_spec.rb +++ b/spec/rubocop/cop/rspec/repeated_subject_call_spec.rb @@ -114,6 +114,17 @@ RUBY end + it 'does not register an offense when `subject` as an argument' do + expect_no_offenses(<<~RUBY) + RSpec.describe Foo do + it do + expect { create(:bar, baz: subject) }.to change { A.count } + expect { create(:bar, subject) }.to not_change { A.count } + end + end + RUBY + end + it 'does not register an offense when `subject` with not expectation' do expect_no_offenses(<<~RUBY) RSpec.describe Foo do