Skip to content

Commit

Permalink
Fix a false positive for Lint/SuppressedException
Browse files Browse the repository at this point in the history
This PR fixes a false positive for `Lint/SuppressedException` when
empty rescue with comment in `def`.

```console
% cat example.rb
def foo
  do_something
rescue
  # noop
end

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

Inspecting 1 file
W

Offenses:

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

1 file inspected, 1 offense detected
```

The above code should not be warned because `AllowComments: true` by defualt.
  • Loading branch information
koic committed May 24, 2020
1 parent b3e38e6 commit b5d9ae1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#8008](https://github.com/rubocop-hq/rubocop/issues/8008): Fix an error for `Lint/SuppressedException` when empty rescue block in `def`. ([@koic][])
* [#8012](https://github.com/rubocop-hq/rubocop/issues/8012): Fix an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant` when deprecated OpenSSL constant is used in a block. ([@koic][])
* [#8017](https://github.com/rubocop-hq/rubocop/pull/8017): Fix a false positive for `Lint/SuppressedException` when empty rescue with comment in `def`. ([@koic][])

## 0.84.0 (2020-05-21)

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) do |ancestor|
node.each_ancestor(:kwbegin, :def) do |ancestor|
end_line = ancestor.loc.end.line
break
end
Expand Down
51 changes: 43 additions & 8 deletions spec/rubocop/cop/lint/suppressed_exception_spec.rb
Expand Up @@ -25,6 +25,29 @@
end
RUBY
end

context 'when empty rescue for `def`' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
def 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 foo
do_something
rescue
^^^^^^ Do not suppress exceptions.
# do nothing
end
RUBY
end
end
end

context 'with AllowComments set to true' do
Expand All @@ -41,14 +64,26 @@
RUBY
end

it 'registers an offense for empty rescue block in `def`' do
expect_offense(<<~RUBY)
def foo
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
context 'when empty rescue for `def`' do
it 'registers an offense for empty rescue without comment' do
expect_offense(<<~RUBY)
def 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 foo
do_something
rescue
# do nothing
end
RUBY
end
end

it 'registers an offense for empty rescue on single line with a comment after it' do
Expand Down

0 comments on commit b5d9ae1

Please sign in to comment.