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 #8081] Fix a false positive for Lint/SuppressedException #8082

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 @@ -5,6 +5,7 @@
### Bug fixes

* [#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 an error for `Lint/SuppressedException` when empty rescue block in `do` block. ([@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) do |ancestor|
node.each_ancestor(:kwbegin, :def, :block) do |ancestor|
end_line = ancestor.loc.end.line
break
end
Expand Down
47 changes: 47 additions & 0 deletions spec/rubocop/cop/lint/suppressed_exception_spec.rb
Expand Up @@ -48,6 +48,30 @@ def foo
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
expect_offense(<<~RUBY)
foo do
do_something
rescue
^^^^^^ Do not suppress exceptions.
end
RUBY
end

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

context 'with AllowComments set to true' do
Expand Down Expand Up @@ -86,6 +110,29 @@ def foo
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
expect_offense(<<~RUBY)
foo do
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)
foo do
rescue
# do nothing
end
RUBY
end
end
end

it 'registers an offense for empty rescue on single line with a comment after it' do
expect_offense(<<~RUBY)
RSpec.describe Dummy do
Expand Down