Skip to content

Commit

Permalink
Merge pull request #1185 from rubocop/fix-iterated-expectation-false-…
Browse files Browse the repository at this point in the history
…positive

[Fix #1184] false positive when block do not have child nodes
  • Loading branch information
Darhazer committed Aug 25, 2021
2 parents 2ff7294 + 99884b1 commit fb6ad3b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,8 +7,9 @@
* Exclude unrelated Rails directories from `RSpec/DescribeClass`. ([@MothOnMars][])
* Add `RSpec/ExcessiveDocstringSpacing` cop. ([@G-Rath][])
* Add `RSpec/SubjectDeclaration` cop. ([@dswij][])
* Fix excessive whitespace removal in `RSpec/EmptyHook' autocorrection. ([@pirj][])
* Fix excessive whitespace removal in `RSpec/EmptyHook` autocorrection. ([@pirj][])
* Bump RuboCop requirement to v1.19.0. ([@pirj][])
* Fix false positive in `RSpec/IteratedExpectation` when there is single, non-espectation statement in the block body. ([@Darhazer][])

## 2.4.0 (2021-06-09)

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rspec/iterated_expectation.rb
Expand Up @@ -48,6 +48,8 @@ def single_expectation?(body, arg)
end

def only_expectations?(body, arg)
return false unless body.each_child_node.any?

body.each_child_node.all? { |child| expectation?(child, arg) }
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rspec/iterated_expectation_spec.rb
Expand Up @@ -27,6 +27,14 @@
RUBY
end

it 'ignores `each` with unused variable' do
expect_no_offenses(<<-RUBY)
it 'validates users' do
[user1, user2, user3].each { |_user| do_something }
end
RUBY
end

it 'flags `each` with multiple expectations' do
expect_offense(<<-RUBY)
it 'validates users' do
Expand Down

0 comments on commit fb6ad3b

Please sign in to comment.