From 99884b197095ca69cdb6472de0ed634a000d578f Mon Sep 17 00:00:00 2001 From: Maxim Krizhanovski Date: Tue, 24 Aug 2021 16:10:28 +0100 Subject: [PATCH] [Fix #1184] false positive when block do not have child nodes --- CHANGELOG.md | 3 ++- lib/rubocop/cop/rspec/iterated_expectation.rb | 2 ++ spec/rubocop/cop/rspec/iterated_expectation_spec.rb | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 442ff6dfa..5709637bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/rspec/iterated_expectation.rb b/lib/rubocop/cop/rspec/iterated_expectation.rb index 8c538494e..58bef7acd 100644 --- a/lib/rubocop/cop/rspec/iterated_expectation.rb +++ b/lib/rubocop/cop/rspec/iterated_expectation.rb @@ -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 diff --git a/spec/rubocop/cop/rspec/iterated_expectation_spec.rb b/spec/rubocop/cop/rspec/iterated_expectation_spec.rb index 5a5d3846c..74aecf0c1 100644 --- a/spec/rubocop/cop/rspec/iterated_expectation_spec.rb +++ b/spec/rubocop/cop/rspec/iterated_expectation_spec.rb @@ -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