Skip to content

Commit a59315f

Browse files
committed
fix false positive on UnspecifiedException cop when function is named raise_exception
1 parent 16cf19c commit a59315f

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
- Fix false-positive for `RSpec/UnspecifiedException` when a method is literally named `raise_exception`. ([@aarestad])
6+
57
## 3.0.5 (2024-09-07)
68

79
- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
@@ -899,6 +901,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
899901

900902
<!-- Contributors (alphabetically) -->
901903

904+
[@aarestad]: https://github.com/aarestad
902905
[@abrom]: https://github.com/abrom
903906
[@ahukkanen]: https://github.com/ahukkanen
904907
[@akiomik]: https://github.com/akiomik

lib/rubocop/cop/rspec/unspecified_exception.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def empty_exception_matcher?(node)
6262
end
6363

6464
def find_expect_to(node)
65-
node.each_ancestor(:send).find do |ancestor|
65+
node.each_ancestor.find do |ancestor|
66+
break if ancestor.block_type?
67+
next unless ancestor.send_type?
68+
6669
expect_to?(ancestor)
6770
end
6871
end

spec/rubocop/cop/rspec/unspecified_exception_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@
8585
}.to raise_error(my_exception)
8686
RUBY
8787
end
88+
89+
it 'allows a subject function to be named raise_error' do
90+
expect_no_offenses(<<~RUBY)
91+
def raise_exception
92+
raise StandardError
93+
end
94+
95+
expect {
96+
raise_error
97+
}.to raise_error(StandardError)
98+
RUBY
99+
end
100+
101+
it 'allows a subject function to be named raise_exception' do
102+
expect_no_offenses(<<~RUBY)
103+
def raise_exception
104+
raise StandardError
105+
end
106+
107+
expect {
108+
raise_exception
109+
}.to raise_error(StandardError)
110+
RUBY
111+
end
88112
end
89113

90114
context 'with raise_exception matcher' do
@@ -198,5 +222,29 @@
198222
^^^^^^^^^^^^^^^ Specify the exception being captured
199223
RUBY
200224
end
225+
226+
it 'allows a subject function to be named raise_exception' do
227+
expect_no_offenses(<<~RUBY)
228+
def raise_error
229+
raise StandardError
230+
end
231+
232+
expect {
233+
raise_exception
234+
}.to raise_exception(StandardError)
235+
RUBY
236+
end
237+
238+
it 'allows a subject function to be named raise_error' do
239+
expect_no_offenses(<<~RUBY)
240+
def raise_error
241+
raise StandardError
242+
end
243+
244+
expect {
245+
raise_error
246+
}.to raise_exception(StandardError)
247+
RUBY
248+
end
201249
end
202250
end

0 commit comments

Comments
 (0)