From d0bb28b48f8bb8919546ae87f774ee9b49f61fef Mon Sep 17 00:00:00 2001 From: Serhii Konev Date: Tue, 9 Apr 2024 14:40:24 +0300 Subject: [PATCH 1/2] RSpec/RepeatedSubjectCall: do not report if subject is an argument to a function call --- CHANGELOG.md | 2 ++ lib/rubocop/cop/rspec/repeated_subject_call.rb | 1 + spec/rubocop/cop/rspec/repeated_subject_call_spec.rb | 11 +++++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9a12b28b..49ba283e4 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) @@ -978,3 +979,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@ypresto]: https://github.com/ypresto [@zdennis]: https://github.com/zdennis [@zverok]: https://github.com/zverok +[@K-S-A]: https://github.com/K-S-A 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 From b038948419f836c27b93d85eb3939d86de73aa16 Mon Sep 17 00:00:00 2001 From: Serhii Konev Date: Tue, 9 Apr 2024 14:40:24 +0300 Subject: [PATCH 2/2] Fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49ba283e4..bcd1baa22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -911,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 @@ -979,4 +980,3 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@ypresto]: https://github.com/ypresto [@zdennis]: https://github.com/zdennis [@zverok]: https://github.com/zverok -[@K-S-A]: https://github.com/K-S-A