Skip to content

Commit

Permalink
RSpec/RepeatedSubjectCall: do not report if subject is an argument to…
Browse files Browse the repository at this point in the history
… a function call
  • Loading branch information
K-S-A committed Apr 10, 2024
1 parent 7b2884e commit d0bb28b
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 @@ -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
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 d0bb28b

Please sign in to comment.