Skip to content

Commit

Permalink
[Fix #11682] Fix a false positive for Lint/UselessRescue
Browse files Browse the repository at this point in the history
Fixes #11682.

This PR fixes a false positive for `Lint/UselessRescue`
when using `Thread#raise` in `rescue` clause.
In fact, any method call with a receiver will not be "useless rescue",
not just `Thread#raise`.
  • Loading branch information
koic authored and bbatsov committed Mar 10, 2023
1 parent 94fdf21 commit 6c3bbff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_lint_useless_rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11682](https://github.com/rubocop/rubocop/issues/11682): Fix a false positive for `Lint/UselessRescue` when using `Thread#raise` in `rescue` clause. ([@koic][])
5 changes: 4 additions & 1 deletion lib/rubocop/cop/lint/useless_rescue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ def on_rescue(node)

private

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
def only_reraising?(resbody_node)
return false if use_exception_variable_in_ensure?(resbody_node)

body = resbody_node.body
return false if body.nil? || !body.send_type? || !body.method?(:raise)

return false if body.nil? || !body.send_type? || !body.method?(:raise) || body.receiver
return true unless body.arguments?
return false if body.arguments.size > 1

exception_name = body.first_argument.source

exception_objects(resbody_node).include?(exception_name)
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity

def use_exception_variable_in_ensure?(resbody_node)
return false unless (exception_variable = resbody_node.exception_variable)
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/lint/useless_rescue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ def foo
RUBY
end

it 'does not register an offense when using `Thread#raise` in `rescue` clause' do
expect_no_offenses(<<~RUBY)
def foo
do_something
rescue
Thread.current.raise
end
RUBY
end

it 'does not register an offense when using modifier `rescue`' do
expect_no_offenses(<<~RUBY)
do_something rescue nil
Expand Down

0 comments on commit 6c3bbff

Please sign in to comment.