Skip to content

Commit

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

This PR fixes a false positive for `Lint/SuppressedException`
when empty rescue block in defs.

```console
% cat example.rb
def self.foo
rescue Foo
  # ok
end

% bundle exec rubocop --only Lint/SuppressedException
(snip)

Inspecting 1 file
W

Offenses:

example.rb:2:1: W: Lint/SuppressedException: Do not suppress exceptions.
rescue Foo
^^^^^^^^^^

1 file inspected, 1 offense detected
```
  • Loading branch information
koic authored and bbatsov committed Jun 5, 2020
1 parent b52b6e4 commit 1f65829
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#8083](https://github.com/rubocop-hq/rubocop/issues/8083): Fix an error for `Lint/MixedRegexpCaptureTypes` cop when using a regular expression that cannot be processed by regexp_parser gem. ([@koic][])
* [#8081](https://github.com/rubocop-hq/rubocop/issues/8081): Fix a false positive for `Lint/SuppressedException` when empty rescue block in `do` block. ([@koic][])
* [#8096](https://github.com/rubocop-hq/rubocop/issues/8096): Fix a false positive for `Lint/SuppressedException` when empty rescue block in defs. ([@koic][])

## 0.85.0 (2020-06-01)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/suppressed_exception.rb
Expand Up @@ -78,7 +78,7 @@ def on_resbody(node)

def comment_between_rescue_and_end?(node)
end_line = nil
node.each_ancestor(:kwbegin, :def, :block) do |ancestor|
node.each_ancestor(:kwbegin, :def, :defs, :block) do |ancestor|
end_line = ancestor.loc.end.line
break
end
Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cop/lint/suppressed_exception_spec.rb
Expand Up @@ -49,6 +49,29 @@ def foo
end
end

context 'when empty rescue for defs' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
def self.foo
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end

it 'registers an offense for empty rescue with comment' do
expect_offense(<<~RUBY)
def self.foo
do_something
rescue
^^^^^^ Do not suppress exceptions.
# do nothing
end
RUBY
end
end

context 'Ruby 2.5 or higher', :ruby25 do
context 'when empty rescue for `do` block' do
it 'registers an offense for empty rescue without comment' do
Expand Down Expand Up @@ -110,6 +133,28 @@ def foo
end
end

context 'when empty rescue for `defs`' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
def self.foo
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end

it 'does not register an offense for empty rescue with comment' do
expect_no_offenses(<<~RUBY)
def self.foo
do_something
rescue
# do nothing
end
RUBY
end
end

context 'Ruby 2.5 or higher', :ruby25 do
context 'when empty rescue for `do` block' do
it 'registers an offense for empty rescue without comment' do
Expand Down

0 comments on commit 1f65829

Please sign in to comment.