Skip to content

Commit c43909d

Browse files
committed
Fix false positive for RSpec/EmptyExampleGroup with iterator and simple conditional
1 parent 0b65016 commit c43909d

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix `RSpec/SortMetadata` cop to limit sorting to trailing metadata arguments. ([@cbliard])
66
- Replace `RSpec/StringAsInstanceDoubleConstant` with `RSpec/VerifiedDoubleReference` configured to only support constant class references. ([@corsonknowles])
7+
- Fix `Rspec/EmptyExampleGroup` cop false positive when a simple conditional is used inside an iterator. ([@lovro-bikic])
78

89
## 3.3.0 (2024-12-12)
910

@@ -992,6 +993,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
992993
[@leoarnold]: https://github.com/leoarnold
993994
[@liberatys]: https://github.com/Liberatys
994995
[@lokhi]: https://github.com/lokhi
996+
[@lovro-bikic]: https://github.com/lovro-bikic
995997
[@luke-hill]: https://github.com/luke-hill
996998
[@m-yamashita01]: https://github.com/M-Yamashita01
997999
[@marocchino]: https://github.com/marocchino

lib/rubocop/cop/rspec/empty_example_group.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class EmptyExampleGroup < Base
130130
def_node_matcher :examples?, <<~PATTERN
131131
{
132132
#examples_directly_or_in_block?
133+
#examples_in_branches?
133134
(begin <#examples_directly_or_in_block? ...>)
134135
(begin <#examples_in_branches? ...>)
135136
}
@@ -170,6 +171,7 @@ def conditionals_with_examples?(body)
170171
end
171172

172173
def examples_in_branches?(condition_node)
174+
return false unless condition_node
173175
return false if !condition_node.if_type? && !condition_node.case_type?
174176

175177
condition_node.branches.any? { |branch| examples?(branch) }

spec/rubocop/cop/rspec/empty_example_group_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,29 @@
225225
end
226226

227227
it 'ignores example group with examples defined in `if` branch ' \
228-
'inside iterator' do
228+
'inside iterator with begin block' do
229229
expect_no_offenses(<<~RUBY)
230230
describe 'RuboCop monthly' do
231231
[1, 2, 3].each do |page|
232232
version = 2.3
233233
234234
if RUBY_VERSION >= version
235-
it { expect(use_safe_navigation_operator?(code)).to be(true) }
235+
it { expect(newspaper(page)).to have_ads }
236+
else
237+
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
238+
end
239+
end
240+
end
241+
RUBY
242+
end
243+
244+
it 'ignores example group with examples defined in `if` branch ' \
245+
'inside iterator without begin block' do
246+
expect_no_offenses(<<~RUBY)
247+
describe 'RuboCop monthly' do
248+
[1, 2, 3].each do |page|
249+
if RUBY_VERSION >= 2.3
250+
it { expect(newspaper(page)).to have_ads }
236251
else
237252
warn 'Ruby < 2.3 is barely supported, please use a newer version for development.'
238253
end

0 commit comments

Comments
 (0)