Skip to content

Commit

Permalink
Merge pull request #1871 from K-S-A/master
Browse files Browse the repository at this point in the history
RSpec/RepeatedSubjectCall: silence subject passed as function argument
  • Loading branch information
bquorning committed Apr 10, 2024
2 parents 26fd2dd + b038948 commit 863bafa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
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

0 comments on commit 863bafa

Please sign in to comment.