Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for Lint/SuppressedException #8017

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
koic marked this conversation as resolved.
Show resolved Hide resolved
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