Skip to content

Commit

Permalink
Merge pull request #8017 from koic/fix_false_positive_for_lint_suppre…
Browse files Browse the repository at this point in the history
…ssed_exception

Fix a false positive for `Lint/SuppressedException`
  • Loading branch information
koic committed May 25, 2020
2 parents a3ebf71 + b5d9ae1 commit 8a383ac
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 8a383ac

Please sign in to comment.