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

RSpec/RepeatedSubjectCall: silence subject passed as function argument #1871

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/repeated_subject_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rspec/repeated_subject_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down