From b5d9ae1f73650481f72310ef768eb9de04740410 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 23 May 2020 19:43:37 +0900 Subject: [PATCH] Fix a false positive for `Lint/SuppressedException` 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. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/suppressed_exception.rb | 2 +- .../cop/lint/suppressed_exception_spec.rb | 51 ++++++++++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e41d2b702e..e689b7ca109 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/suppressed_exception.rb b/lib/rubocop/cop/lint/suppressed_exception.rb index 2d7e1f7f1a5..eebf466496b 100644 --- a/lib/rubocop/cop/lint/suppressed_exception.rb +++ b/lib/rubocop/cop/lint/suppressed_exception.rb @@ -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 diff --git a/spec/rubocop/cop/lint/suppressed_exception_spec.rb b/spec/rubocop/cop/lint/suppressed_exception_spec.rb index 4bb62f7f67d..151bf8453d5 100644 --- a/spec/rubocop/cop/lint/suppressed_exception_spec.rb +++ b/spec/rubocop/cop/lint/suppressed_exception_spec.rb @@ -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 @@ -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