diff --git a/CHANGELOG.md b/CHANGELOG.md index 34a336446d6..86585f83c4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#8008](https://github.com/rubocop-hq/rubocop/issues/8008): Fix an error for `Lint/SuppressedException` when empty rescue block in `def`. ([@koic][]) + ## 0.84.0 (2020-05-21) ### New features diff --git a/lib/rubocop/cop/lint/suppressed_exception.rb b/lib/rubocop/cop/lint/suppressed_exception.rb index aaad8c27110..2d7e1f7f1a5 100644 --- a/lib/rubocop/cop/lint/suppressed_exception.rb +++ b/lib/rubocop/cop/lint/suppressed_exception.rb @@ -82,6 +82,8 @@ def comment_between_rescue_and_end?(node) end_line = ancestor.loc.end.line break end + return false unless end_line + processed_source[node.first_line...end_line].any? { |line| comment_line?(line) } end end diff --git a/spec/rubocop/cop/lint/suppressed_exception_spec.rb b/spec/rubocop/cop/lint/suppressed_exception_spec.rb index 4a5bb488a59..4bb62f7f67d 100644 --- a/spec/rubocop/cop/lint/suppressed_exception_spec.rb +++ b/spec/rubocop/cop/lint/suppressed_exception_spec.rb @@ -41,6 +41,16 @@ 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 + end + it 'registers an offense for empty rescue on single line with a comment after it' do expect_offense(<<~RUBY) RSpec.describe Dummy do