From 00f7941ff7bab36ebf6ae2dc200813b59f7cecaa Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 2 Jun 2020 16:44:44 +0900 Subject: [PATCH] [Fix #8081] Fix an error for `Lint/SuppressedException` Fixes #8081. This PR fixes an error for `Lint/SuppressedException` when empty rescue block in `do` block. ```console % cat example.rb foo do rescue Foo # ok end % bundle exec rubocop --only Lint/SuppressedException (snip) Inspecting 1 file W Offenses: example.rb:2:1: W: Lint/SuppressedException: Do not suppress exceptions. rescue Foo ^^^^^^^^^^ 1 file inspected, 1 offense detected ``` `do` block rescue is a syntax introduced in Ruby 2.5. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/suppressed_exception.rb | 2 +- .../cop/lint/suppressed_exception_spec.rb | 47 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29fc64351ea..d7700633186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/suppressed_exception.rb b/lib/rubocop/cop/lint/suppressed_exception.rb index 1270115a428..3cea4b9f36b 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, :def) do |ancestor| + node.each_ancestor(:kwbegin, :def, :block) 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 151bf8453d5..d0ac886257d 100644 --- a/spec/rubocop/cop/lint/suppressed_exception_spec.rb +++ b/spec/rubocop/cop/lint/suppressed_exception_spec.rb @@ -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 @@ -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