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

[Fix #1184] false positive when block do not have child nodes #1185

Merged
merged 1 commit into from Aug 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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